5

我尝试在服务中使用意图但是当我尝试这个时:

Intent intent_facebook = new Intent (this,MainUploadToYoutube.class);
intent_facebook.putExtra("vid", vid);
startActivity(intent_facebook);

在 logcat 上收到此错误:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

所以我从这里尝试了这个:

android从服务开始活动

Intent  intent_facebook = new Intent(getBaseContext(), MainUploadToYoutube.class);
intent_facebook.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity( intent_facebook);

但这什么也没做,我在 logcat 中没有收到错误

怎么了?

4

3 回答 3

3

Have you tried your own code (using this as context), but just add the flags as the error tells you?

Intent intent_facebook = new Intent (this, MainUploadToYoutube.class);
intent_facebook.putExtra("vid", vid);
intent_facebook.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent_facebook);
于 2013-05-13T19:40:44.010 回答
0

这可能会有所帮助

Service课堂上你得到context并初始化Context mContext

 Intent intent = new Intent(mContext,MainUploadToYoutube.class);   
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
 ((Activity)mContext).startActivity(intent);
于 2013-05-13T10:43:21.853 回答
0

您的代码没有任何问题。它应该工作。你的问题是别的。确保在清单中定义 MainUploadToYoutube 活动,并且一旦此活动启动,应用程序可能不会崩溃。

于 2013-05-13T11:41:50.143 回答