我有一个 GCM 服务,我想创建一个 Intent 选择器(使用Intent.createChooser()
),但是尽管我一直在尝试各种事情,但我总是得到Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
我做错了什么或者你只是没有从服务中做到这一点?
该服务的代码如下(我尝试过的各种事情中有一些评论);
public class GCMIntentService extends GCMBaseIntentService
{
// ...
protected void onMessage(Context context, Intent intent)
{
Log.i(TAG, "Received message");
String message = "Push received";
displayMessage(context, message);
fireIntent(context, message);
}
private void fireIntent(Context context, String url)
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, url);
//intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "Share URL"));
//context.startActivity(Intent.createChooser(intent, "Share URL"));
//sendBroadcast(intent);
}
}
提前致谢。