AruLNadhaN
AruLNadhaN

Reputation: 2826

Which Context should i use here?

I am developing a App using Youtube API. I don't know what context should i pass in the Intent.I tried this, getApplicationContext(),VideoActivity.thisand the one used below. I Already referred some similar questions but they are not helping me . So Please somebody tell me what are differences between these contexts.

public class VideoActivity extends Activity {

private static final String TAG = "FeedListActivity";


private static final int REQ_START_STANDALONE_PLAYER = 1;
private static final int REQ_RESOLVE_SERVICE_MISSING = 2;

private static final String PLAYLIST_ID = "*******";
public static final String DEVELOPER_KEY = "*******";

public ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
public String mvideoid;
private String URL_FEED = "http://www.amsoin.net/ins/nes.json";
Context mContext;
public VideoActivity (Context context){
    mContext = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_feed_list);
    listView = (ListView) findViewById(R.id.feed_list);
    feedItems = new ArrayList<FeedItem>();
    listAdapter = new FeedListAdapter(this, feedItems);
    listView.setAdapter(listAdapter);
    // making fresh volley request and getting json
    GsonRequest<FeedResult> gsonRequest = new GsonRequest<FeedResult>(URL_FEED, FeedResult.class,
            new Response.Listener<FeedResult>() {
                @Override
                public void onResponse(FeedResult response) {
                    feedItems = response.getFeedItems();
                    listAdapter.setData(feedItems);
                    listAdapter.notifyDataSetChanged();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "Error: " + error.getMessage());
                }
            });


    // Adding request to volley request queue
    AppController.getInstance().addRequest(gsonRequest, TAG);
}


public void Play(View v) {

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
            Log.d(TAG,mvideoid);
            int startIndex = 0;
            int startTimeMillis = 0;
            boolean autoplay = true;
            boolean lightboxMode = false;

            Intent intent = null;
            **intent = YouTubeStandalonePlayer.createPlaylistIntent(mContext, DEVELOPER_KEY,
            PLAYLIST_ID, startIndex, startTimeMillis, autoplay, lightboxMode);**
            startActivity(intent);

            if (intent != null) {
                if (canResolveIntent(intent)) {
                    startActivityForResult(intent, REQ_START_STANDALONE_PLAYER);
                } else {
                    // Could not resolve the intent - must need to install or update the YouTube API service.
                    YouTubeInitializationResult.SERVICE_MISSING
                            .getErrorDialog(mContext, REQ_RESOLVE_SERVICE_MISSING).show();
                }
            }


        }
    });
}

Upvotes: 0

Views: 92

Answers (1)

Davidsun
Davidsun

Reputation: 721

First of all, in your code

public VideoActivity (Context context){
    mContext = context;
}

is not the correct usage of Activity. Activity's constructors shouldn't be overwritten as it's maintained by Android framework.

The difference between Context(s), is their life cycles. For an Activity, it can be destroyed as soon as it's finished, but for an application context, it will be there as long as your application is running.

Upvotes: 2

Related Questions