Broadcastreceiver 发出的通知在被反压时关闭应用程序
Fired notification from Broadcastreceiver is closing app when backpressed
好吧,我有一个应用程序,它基本上包含三个 3 classes,即 1-MainActivity、2-DetailActivity 和 3-Broadcast。即使应用程序已关闭,广播接收器也在工作,如果它收到某些东西,它会触发通知。当我按下通知时,它正在打开详细信息 class。到目前为止,没有问题,它工作完美。但如果我在 DetailActivity 中按回。它指向我 phone 的主页。但是应用程序应该将我引导至 Main class。如果我从后台返回应用程序 运行 应用程序将我引导到主页后,它将启动主应用程序 class 我使用的文档代码是:
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
和
int id = 1;
...
Intent resultIntent = new Intent(this, DetailActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(DetailActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
如@MeknessiHamida 所述..您应该重写 OnBackPressed
方法并开始启动主 activity.
的意图
你只需要在细节中覆盖它Activity(根据你的问题):制定一个将你定向到主要内容的意图Activity:
@Override public void onBackPressed() {
//Include the code here
return; }
实际上我在尝试和错误之后找到了答案,
PendingIntent.FLAG_UPDATE_CURRENT
更改此行后它工作正常,而且我正在使用 getApplicationContext() 而不是“.this”;对于上下文
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_IMMUTABLE
);
好吧,我有一个应用程序,它基本上包含三个 3 classes,即 1-MainActivity、2-DetailActivity 和 3-Broadcast。即使应用程序已关闭,广播接收器也在工作,如果它收到某些东西,它会触发通知。当我按下通知时,它正在打开详细信息 class。到目前为止,没有问题,它工作完美。但如果我在 DetailActivity 中按回。它指向我 phone 的主页。但是应用程序应该将我引导至 Main class。如果我从后台返回应用程序 运行 应用程序将我引导到主页后,它将启动主应用程序 class 我使用的文档代码是:
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
和
int id = 1;
...
Intent resultIntent = new Intent(this, DetailActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(DetailActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
如@MeknessiHamida 所述..您应该重写 OnBackPressed
方法并开始启动主 activity.
你只需要在细节中覆盖它Activity(根据你的问题):制定一个将你定向到主要内容的意图Activity:
@Override public void onBackPressed() {
//Include the code here
return; }
实际上我在尝试和错误之后找到了答案,
PendingIntent.FLAG_UPDATE_CURRENT
更改此行后它工作正常,而且我正在使用 getApplicationContext() 而不是“.this”;对于上下文
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_IMMUTABLE
);