是否有可能推出一个活动或提高从应用程序类的静态方法的意图是什么? [英] Is it possible to launch an Activity or raise an Intent from static method in Application class?
问题描述
我有自定义应用程序的类
I have custom app 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"));
}
}
当我把这种方法,我收到此错误信息
and when I call this method I receive this error message
从活动背景以外调用startActivity()要求 在FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要什么?
"Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?"
是的,它是真正想要的,但我为什么不能做呢?
YES it is that what really want but why I can not do it ?
推荐答案
当你调用Intent.createChooser(),它返回一个新的ACTION_CHOOSER意向书不具有FLAG_ACTIVITY_NEW_TASK集。这就是为什么你收到此错误。也许你想是这样的:
When you call Intent.createChooser(), this returns a new ACTION_CHOOSER Intent which does NOT have the FLAG_ACTIVITY_NEW_TASK set. That's why you are getting this error. Maybe you want something like this:
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);
}
这篇关于是否有可能推出一个活动或提高从应用程序类的静态方法的意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!