gpt4 book ai didi

Android 通知恢复 Activity

转载 作者:太空狗 更新时间:2023-10-29 16:20:17 34 4
gpt4 key购买 nike

我正在尝试在用户暂停我的应用程序时发出通知。因此,为了更容易,用户可以使用通知快速转到应用程序。这是我正在使用的代码。它适用于 android 4 之前的所有版本,我不知道是哪个问题

 NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Titulo")
.setContentText("Titulo");

mBuilder.setOngoing(true);

// Creates an explicit intent for an Activity this
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
// put the flags
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

因此,当我在 android 4.0 及更高版本中按下通知时,将再次创建 Activity 而不是恢复。请提供任何帮助,我无法让它工作。

编辑(忘记单机 list )

android:launchMode="singleTop" 同样的结果,不工作...

我的 Activity 包含一张 map 。我正在使用新版本的谷歌地图。 v2.

最佳答案

我刚刚尝试了 PendingIntent.FLAG_CANCEL_CURRENT 并且似乎对我有用

public void showNotification(String header,String message){

// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Intent intent = new Intent(this, MainActivity.class);

//PendingIntent.FLAG_CANCEL_CURRENT will bring the app back up again
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,PendingIntent.FLAG_CANCEL_CURRENT, intent, 0);

Notification mNotification = new Notification.Builder(this)
.setContentTitle(header)
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSound(soundUri)

.addAction(R.drawable.ic_launcher, "View", pIntent)
.addAction(0, "Remind", pIntent)
.setOngoing(true)//optional
.build();

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, mNotification);
}

关于Android 通知恢复 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16933597/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com