個人のブログ
標準モードでのアプリケーション起動分析アクティビティエラー
Android
開始Activity
に対応するタスク・スタックで実行されます。あなたが直接場合はApplication
する标准模式
活動を開始し、それが次のエラー報告します(Android8除くAndroid7を、後に分析されます):
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:912)
at android.app.ContextImpl.startActivity(ContextImpl.java:888)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:379)
エラーメッセージが促さFLAG_ACTIVITY_NEW_TASK
の旗を。
通話中の活性getApplication()
又はgetApplicationContext()
最終的に取得されたが、その後、適用されgetApplication().startActivity()
またはgetApplicationContext().startActivity()
適用方法
@Override
public void startActivity(Intent intent) {
mBase.startActivity(intent);
}
startActivity年における法の適用は、渡されたmBase
startActivityを呼び出すために、そしてmBaseですContextImpl
。startActivityのContextImpl方法
@Override
public void startActivity(Intent intent) {
warnIfCallingFromSystemProcess();
startActivity(intent, null);
}
このメソッドは、別のオーバーロードされたメソッドを呼び出していました
//Android6
@Override
public void startActivity(Intent intent, Bundle options) {
warnIfCallingFromSystemProcess();
if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
throw new AndroidRuntimeException(
"Calling startActivity() from outside of an Activity "
+ " context requires the FLAG_ACTIVITY_NEW_TASK flag."
+ " Is this really what you want?");
}
mMainThread.getInstrumentation().execStartActivity(
getOuterContext(), mMainThread.getApplicationThread(), null,
(Activity) null, intent, -1, options);
}
Android6は、この方法では、我々は以前のスローで例外を見ることができます。増加していない場合は、この判決では、FLAG_ACTIVITY_NEW_TASK
旗を、それが例外をスローします。
ルック方法Android7
@Override
public void startActivity(Intent intent, Bundle options) {
warnIfCallingFromSystemProcess();
// Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
// generally not allowed, except if the caller specifies the task id the activity should
// be launched in.
if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
&& options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
throw new AndroidRuntimeException(
"Calling startActivity() from outside of an Activity "
+ " context requires the FLAG_ACTIVITY_NEW_TASK flag."
+ " Is this really what you want?");
}
mMainThread.getInstrumentation().execStartActivity(
getOuterContext(), mMainThread.getApplicationThread(), null,
(Activity) null, intent, -1, options);
}
また、他のいくつかの条件を満たすために必要投げ、Android7、比較的Android6で見ることができます。ビューstartActivity適用方法の観点から上記のオプションパラメータのバンドルタイプは、この条件が満たされていないので、例外がスローされていない、ヌルです。
ルック方法Android8
@Override
public void startActivity(Intent intent, Bundle options) {
warnIfCallingFromSystemProcess();
// Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
// generally not allowed, except if the caller specifies the task id the activity should
// be launched in.
if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
&& options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
throw new AndroidRuntimeException(
"Calling startActivity() from outside of an Activity "
+ " context requires the FLAG_ACTIVITY_NEW_TASK flag."
+ " Is this really what you want?");
}
mMainThread.getInstrumentation().execStartActivity(
getOuterContext(), mMainThread.getApplicationThread(), null,
(Activity) null, intent, -1, options);
}
そしてAndroid7オプションがヌルであるため、選択肢を決定することも好きで、そのための条件が満たされていない、それが例外をスローしません。
Android9の方法で見てみましょう
@Override
public void startActivity(Intent intent, Bundle options) {
warnIfCallingFromSystemProcess();
// Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
// generally not allowed, except if the caller specifies the task id the activity should
// be launched in. A bug was existed between N and O-MR1 which allowed this to work. We
// maintain this for backwards compatibility.
final int targetSdkVersion = getApplicationInfo().targetSdkVersion;
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
&& (targetSdkVersion < Build.VERSION_CODES.N
|| targetSdkVersion >= Build.VERSION_CODES.P)
&& (options == null
|| ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) {
throw new AndroidRuntimeException(
"Calling startActivity() from outside of an Activity "
+ " context requires the FLAG_ACTIVITY_NEW_TASK flag."
+ " Is this really what you want?");
}
mMainThread.getInstrumentation().execStartActivity(
getOuterContext(), mMainThread.getApplicationThread(), null,
(Activity) null, intent, -1, options);
}
Android9では、裁判官とオプション== nullの||、そしてActivityOptions.fromBundle(オプション).getLaunchTaskId()== -1、条件のためか、設定されます、または例外がスローされます。
あなたが旗FLAG_ACTIVITY_NEW_TASKを増加することなく、アプリケーションのアクティビティに直接起動することができ、Android7とAndroid8で見ることができます。