【问题标题】:android startActivity from intent in service [duplicate]来自服务意图的android startActivity [重复]
【发布时间】:2013-05-13 10:25:24
【问题描述】:

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

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 start activity from service

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

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

怎么了?

【问题讨论】:

    标签: android android-intent android-service


    【解决方案1】:

    您是否尝试过自己的代码(使用this 作为上下文),但只是按照错误提示添加标志?

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

    【讨论】:

    • 工作 .... 我添加了额外的 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    【解决方案2】:

    这可能会有所帮助

    Service 类中,您将获得context 并使用Context mContext 进行初始化

     Intent intent = new Intent(mContext,MainUploadToYoutube.class);   
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
     ((Activity)mContext).startActivity(intent);
    

    【讨论】:

    • 上下文 mContext = getBaseContext(); Intent intent2 = new Intent(mContext,MainUploadToYoutube.class); intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ((Activity)mContext).startActivity(intent2);我试试这个,但 mcontext 无法转换为活动
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多