从活动背景以外调用startActivity()要求FLAG_ACTIVITY_NEW_TASK背景、startActivity、FLAG_ACTIVITY_NEW_TASK

由网友(读不懂迩的瞳﹏)分享简介:我试图启动一个服务类中的活动。我有以下的code:公共类SendLinkService延伸服务{@覆盖公众的IBinder onBind(意向意图){// TODO自动生成方法存根返回null;}@覆盖公众诠释onStartCommand(意向意图,诠释标志,诠释startId){捆绑包= intent.getE...

我试图启动一个服务类中的活动。我有以下的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);
 
startActivity流程简单分析

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

阅读全文

相关推荐

最新文章