我观察过几个类似的问题,但他们无法帮助我.我需要在用户点击通知时显示上次活动.这是我的代码:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(YourService.this)
.setContentTitle(getResources().getText(R.string.app_name))
.setContentText(getServiceStateDescription(YourService.this))
.setSmallIcon(iconId)
.setWhen(System.currentTimeMillis());
Intent nIntent = getPreviousIntent();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(MainActivity_.class);
stackBuilder.addNextIntent(nIntent);
PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, notificationBuilder.build());
private Intent getPreviousIntent() {
Intent newIntent = null;
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final List<ActivityManager.AppTask> recentTaskInfos = activityManager.getAppTasks();
if (!recentTaskInfos.isEmpty()) {
for (ActivityManager.AppTask appTaskTaskInfo: recentTaskInfos) {
if (appTaskTaskInfo.getTaskInfo().baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
newIntent = appTaskTaskInfo.getTaskInfo().baseIntent;
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
}
} else {
final List<ActivityManager.RecentTaskInfo> …Run Code Online (Sandbox Code Playgroud) 我正在创建像计时器应用程序,当我启动计时器时,我可以选择去Android主页或启动任何其他活动.
当我启动计时器时,我设置了一个通知栏图标,如果我使用其他一些应用程序(意思是从启动计时器活动开始),现在我需要通过点击通知图标来回到我之前启动的计时器活动?
当我点击时,我开始一个新的实例计时器活动,而不是之前启动的计时器活动!,如果我然后单击后退按钮,它会显示我之前的计时器活动..
问题是:如何通过通知栏调用以前启动的活动,而不是启动该活动的新实例?
这是我的代码示例如下:
private void notificationBar()
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ico;
CharSequence tickerText = "some title...";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "some app title";
CharSequence contentText = "...some info !";
Intent notificationIntent = new Intent(this, main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIF_ID, notification);
}
private void notificationClose(int notifID) …Run Code Online (Sandbox Code Playgroud) android android-intent android-notification-bar android-activity
我想通过点击状态栏中的通知来打开上次启动的活动.假设我启动了活动A(我的应用程序的主要活动),此活动会向通知状态栏发送通知.活动A还打开活动B,B打开另一个活动C.从C i按主页按钮.现在我想再次访问我的应用程序,所以从通知栏我点击通知(由A发送).这里通知应该启动活动C,因为它是上次打开的.
我对此进行了搜索但未找到正确的答案.提前致谢.
android ×3