我有自定义应用类
public class MyApp extends Application {
public static Context application_context;
@Override
public void onCreate() {
super.onCreate();
application_context=getApplicationContext();
}
public static void startShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "some text");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
application_context.startActivity(Intent.createChooser(shareIntent,
"Share with"));
}
}
当我调用此方法时,我收到此错误消息
“从Activity上下文外部调用startActivity()需要 FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?“
是的,真正想要的是为什么我不能这样做?
答案 0 :(得分:0)
当您调用Intent.createChooser()时,将返回一个新的ACTION_CHOOSER Intent,它没有设置FLAG_ACTIVITY_NEW_TASK。这就是你得到这个错误的原因。也许你想要这样的东西:
public static void startShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "some text");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
Intent chooserIntent = Intent.createChooser(shareIntent, "Share with");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
application_context.startActivity(ChooserIntent);
}