[英]Avoid re-create activity when click on notification in android
I am now working on a notification reminder, if the user click on the notification , it will open the app. 我现在正在处理通知提醒,如果用户单击通知,它将打开应用程序。
However, the problem is , if I have already open the app, when I click on the notification it still create a new activity, so , there are two activity in total. 但是,问题是,如果我已经打开该应用程序,则在单击通知时它仍会创建一个新活动,因此,总共有两个活动。
How can I achieve like this? 我该如何实现? If the user has already open the app , just do nothing, if not , then open the app. 如果用户已经打开了该应用程序,则什么也不做,否则请打开该应用程序。 Thanks for helping. 感谢您的帮助。
public class AlarmService extends Service {
private static final int MY_NOTIFICATION_ID = 1;
NotificationManager notificationManager;
Notification myNotification;
Vibrator v;
public AlarmService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(5000);
Context context = getApplicationContext();
Intent myIntent = new Intent(this, SplashScreen.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, myIntent, Intent.FLAG_ACTIVITY_SINGLE_TOP);
myNotification = new Notification.Builder(context)
.setContentTitle(getResources().getString(R.string.notify_title))
.setContentText(getResources().getString(R.string.notify_msg) + "\n"
+ getResources().getString(R.string.reminder_1) + "aaa\n"
+ getResources().getString(R.string.reminder_2) + "vvv\n"
+ getResources().getString(R.string.reminder_3) + "cccc\n"
+ getResources().getString(R.string.reminder_5) + "dddd\n")
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(false)
.setSmallIcon(R.drawable.ic_launcher).build();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
Try below code. 尝试下面的代码。
public static void startNotification(Service service, String message) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(service);
if(prefs.getBoolean("pref_NotificationDisplayed", true)){
// Creates an Ongoing Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(service.getApplicationContext()).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Title").setContentText(message);
Intent toLaunch = new Intent(service.getApplicationContext(),MainActivity.class);
toLaunch.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent intentBack = PendingIntent.getActivity(service.getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(intentBack);
NotificationManager mNotificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
// Send Notification
Notification primaryNotification = mBuilder.build();
primaryNotification.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(10001,primaryNotification);
}
}
Thats it... 而已...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.