从活动背景以外调用startActivity()要求FLAG_ACTIVITY_NEW_TASK [英] Calling startActivity() from outside of an activity context requires the FLAG_ACTIVITY_NEW_TASK

查看:149
本文介绍了从活动背景以外调用startActivity()要求FLAG_ACTIVITY_NEW_TASK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图启动一个服务类中的活动。我有以下的code:

 公共类SendLinkService延伸服务{

@覆盖
公众的IBinder onBind(意向意图){
    // TODO自动生成方法存根
    返回null;
}

@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
    捆绑包= intent.getExtras();
    意图shareIntent =新的意图(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.setType(text / plain的);
    shareIntent.putExtra(Intent.EXTRA_TEXT,bundle.getString(URL));
    。getApplicationContext()startActivity(Intent.createChooser(shareIntent,通过分享));
    返回super.onStartCommand(意向,标志,startId);
}
}
 

这对以下onStartCommand()的行给出了异常:

  getApplicationContext()startActivity(Intent.createChooser(shareIntent,通过股份」))。
 

解决方案

试试这个。

 意图shareIntent =新的意图(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setType(text / plain的);
shareIntent.putExtra(Intent.EXTRA_TEXT,bundle.getString(URL));
意图new_intent = Intent.createChooser(shareIntent,通过股份」);
new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
。getApplicationContext()startActivity(new_intent);
 

I am trying to start an activity inside a service class. I have a following code:

public class SendLinkService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle bundle = intent.getExtras();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, bundle.getString("URL"));
    getApplicationContext().startActivity(Intent.createChooser(shareIntent, "Share via"));
    return super.onStartCommand(intent, flags, startId);
}
}

It gives exception on following line of onStartCommand() :

getApplicationContext().startActivity(Intent.createChooser(shareIntent, "Share via"));

解决方案

Try this.

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, bundle.getString("URL"));
Intent new_intent = Intent.createChooser(shareIntent, "Share via");
new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getApplicationContext().startActivity(new_intent);

这篇关于从活动背景以外调用startActivity()要求FLAG_ACTIVITY_NEW_TASK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆