我有一个在前台工作的服务,当主要活动启动时,会使用以下代码启动通知:
private void showNotification() {
Log.i(TAG, LocalService.class.getName() + "showNotification()");
Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity.class);
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// Set the icon, scrolling text and timestamp
int icontouse = R.drawable.icon;
if (app.prefs.getBoolean("prefs_useprivacyicon", false)) {
icontouse = R.drawable.icon_privacy;
}
n = new Notification(icontouse, getString(R.string.voice_pro_ready), System.currentTimeMillis());
n.contentView = getRemoteView();
n.contentIntent = contentIntent;
startForeground(myID, n);
nm.notify(myID, n);
}
我的问题是PendingIntent,如果主要活动处于暂停状态,那么当点击状态栏中的通知时,这会变为前方,但如果主要活动被finish()关闭,则此操作不会显示。
我需要假设如果Activity在内存中而不是可见,如果在内存中不可用,则启动一个新实例。
答案 0 :(得分:1)
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
已更改为notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
再试一次