我应该在这里使用哪种上下文?

时间:2014-10-13 14:55:12

标签: android android-context

我正在使用Youtube API开发应用。我不知道我应该在Intent中传递什么上下文。我尝试了thisgetApplicationContext()VideoActivity.this以及下面使用的内容。 我已经提到了一些类似的问题,但他们没有帮助我。所以请有人告诉我这些背景之间有什么区别。

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();
                }
            }


        }
    });
}

1 个答案:

答案 0 :(得分:2)

首先,在你的代码中

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

不是Activity的正确用法。不应该覆盖Activity的构造函数,因为它是由Android框架维护的。

Context(s)之间的区别在于它们的生命周期。对于Activity,它可以在完成后立即销毁,但对于应用程序上下文,只要您的应用程序正在运行,它就会存在。