[英]Is it possible to launch an Activity or raise an Intent from static method in Application class?
我有自定义应用程序类
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标志。这真的是您想要的吗?”
是的,这是真正想要的,但为什么我不能这样做?
当您调用Intent.createChooser()时,这将返回一个未设置FLAG_ACTIVITY_NEW_TASK的新ACTION_CHOOSER Intent。 这就是为什么您会收到此错误。 也许您想要这样的东西:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.