Android:在不存在新活动时启动新活动
我有一个在前台工作的服务,当主活动启动它时,将使用以下代码启动通知:Android:在不存在新活动时启动新活动,android,Android,我有一个在前台工作的服务,当主活动启动它时,将使用以下代码启动通知: private void showNotification() { Log.i(TAG, LocalService.class.getName() + "showNotification()"); Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity.class); //notificat
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);
}
我的问题是PendingEvent,如果主活动处于暂停状态,那么当单击状态栏中的notify时,它会出现在前面,但是如果主活动是通过finish()关闭的,则不会显示
我需要让活动在内存中变得可见,如果在内存中不可用,则启动一个新实例
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
更改为notificationIntent.setFlags(Intent.FLAG\u ACTIVITY\u SINGLE\u TOP)
然后再试一次您的主要活动的启动模式是什么Yao,我正在使用标准模式尝试删除“notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);”并将活动的启动模式设置为SingleTop