我有一个在前台工作的服务,当主活动启动它时,会使用以下代码启动通知:
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,如果主要活动处于暂停状态,那么当单击状态栏中的通知时,它会在前面,但是如果主要活动被完成()关闭,这不会变得可见。
我需要确保如果 Activity 在内存中变得可见,如果在内存中不可用,则启动一个新实例。