Android - 从通知中重新打开主要 activity
Android - reopen main activity from notification
我的应用程序的启动器 activity 称为 LaunchActivity。在此 activity 中,我检查用户是否已登录。如果是,则进行网络调用以验证用户,如果一切正常,则启动 MainActivity。如果用户第一次运行应用程序或用户验证失败,LoginActivity 运行。
在 LaunchActivity 中有运行适当的函数 activity:
private void start(Class<? extends Activity> startActivity) {
Intent intent = new Intent(this, startActivity);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(intent);
}
应用正在接收通知。通知后单击我想打开应用程序或将其置于顶部(如果它在后台)。
负责通知意图的代码:
Intent intent = new Intent(context, LaunchActivity.class);
intent.putExtra("SHOW_NOTIFICATION_LIST", "");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent notificationIntent = PendingIntent.getActivity(
context,
0,
intent,
0);
builder.setContentIntent(notificationIntent);
Notification notification = builder.build();
问题是,在我当前的实现中,单击后,即使 MainActivity 在后台,LaunchActivity 也会重新打开。我不知道如何让挂起的意图重新打开 MainActivity(没有 LaunchActivity 和验证)如果它在后台或启动 LaunchActivity后台没有 activity 运行(应用不是 运行)。如果有任何帮助,我将不胜感激。
编辑:
清单中的活动声明:
<activity android:name=".activities.LaunchActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.MainActivity"
android:launchMode="singleTop"
android:screenOrientation="userPortrait" />
<activity
android:name=".activities.LoginActivity"
android:excludeFromRecents="true"
android:launchMode="singleTop"
android:noHistory="true"
android:screenOrientation="userPortrait"
android:windowSoftInputMode="adjustResize" >
</activity>
创建一个响应三种不同意图的 BroadcastReceiver
ACTION_CREATED
:将标志设置为真
ACTION_DESTROYED
: 将标志设置为 false
ACTION_LAUNCH
:如果有标志,开始MainActivity
,否则开始LaunchActivity
(标志可以是共享首选项中的布尔值或静态字段)
在MainActivity
中:
- 在
onCreate
中:发送ACTION_CREATED
广播
- 在
onDestroy
中:发送ACTION_DESTROYED
广播
通知应该发送一个ACTION_LAUNCH
广播。
注意:动作可以任意命名。它们也应该以您的包名称开头,这样您就不会干扰其他应用程序。
荒谬行为的原因是因为当你调用 start()
时你正在完成 LaunchActivity
private void start(Class<? extends Activity> startActivity) {
....
finish(); // This is the culprit
....
}
从 start()
中删除 finish()
,它将按预期正常工作。
如果由于您的应用程序行为而无法删除 finish()
,请将您的 PendingIntent
更改为启动 MainActivity
而不是 LaunchActivity
Intent intent = new Intent(context, MainActivity.class);
并在 MainActivity's onCreate()
中检查用户是否已经登录。如果没有,请将他导航到您的 LoginActivity
.
我的应用程序的启动器 activity 称为 LaunchActivity。在此 activity 中,我检查用户是否已登录。如果是,则进行网络调用以验证用户,如果一切正常,则启动 MainActivity。如果用户第一次运行应用程序或用户验证失败,LoginActivity 运行。
在 LaunchActivity 中有运行适当的函数 activity:
private void start(Class<? extends Activity> startActivity) {
Intent intent = new Intent(this, startActivity);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(intent);
}
应用正在接收通知。通知后单击我想打开应用程序或将其置于顶部(如果它在后台)。
负责通知意图的代码:
Intent intent = new Intent(context, LaunchActivity.class);
intent.putExtra("SHOW_NOTIFICATION_LIST", "");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent notificationIntent = PendingIntent.getActivity(
context,
0,
intent,
0);
builder.setContentIntent(notificationIntent);
Notification notification = builder.build();
问题是,在我当前的实现中,单击后,即使 MainActivity 在后台,LaunchActivity 也会重新打开。我不知道如何让挂起的意图重新打开 MainActivity(没有 LaunchActivity 和验证)如果它在后台或启动 LaunchActivity后台没有 activity 运行(应用不是 运行)。如果有任何帮助,我将不胜感激。
编辑:
清单中的活动声明:
<activity android:name=".activities.LaunchActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.MainActivity"
android:launchMode="singleTop"
android:screenOrientation="userPortrait" />
<activity
android:name=".activities.LoginActivity"
android:excludeFromRecents="true"
android:launchMode="singleTop"
android:noHistory="true"
android:screenOrientation="userPortrait"
android:windowSoftInputMode="adjustResize" >
</activity>
创建一个响应三种不同意图的 BroadcastReceiver
ACTION_CREATED
:将标志设置为真ACTION_DESTROYED
: 将标志设置为 falseACTION_LAUNCH
:如果有标志,开始MainActivity
,否则开始LaunchActivity
(标志可以是共享首选项中的布尔值或静态字段)
在MainActivity
中:
- 在
onCreate
中:发送ACTION_CREATED
广播 - 在
onDestroy
中:发送ACTION_DESTROYED
广播
通知应该发送一个ACTION_LAUNCH
广播。
注意:动作可以任意命名。它们也应该以您的包名称开头,这样您就不会干扰其他应用程序。
荒谬行为的原因是因为当你调用 start()
LaunchActivity
private void start(Class<? extends Activity> startActivity) {
....
finish(); // This is the culprit
....
}
从 start()
中删除 finish()
,它将按预期正常工作。
如果由于您的应用程序行为而无法删除 finish()
,请将您的 PendingIntent
更改为启动 MainActivity
而不是 LaunchActivity
Intent intent = new Intent(context, MainActivity.class);
并在 MainActivity's onCreate()
中检查用户是否已经登录。如果没有,请将他导航到您的 LoginActivity
.