如何使用通知操作打开正在运行的活动?

时间:2016-05-04 15:14:06

标签: java android android-studio timer notifications

我想点击通知时打开我的计时器应用程序。我已经尝试过下面的代码了,但它刚刚开始一个新的活动。我想开始已经运行的活动。

Intent intent = new Intent(timer.this, timer.class);
PendingIntent pIntent = PendingIntent.getActivity(timer.this, 0, intent, 0);

notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification = new Notification.Builder(timer.this)
                       .setSmallIcon(R.mipmap.ic_launcher)
                       .setContentIntent(pIntent)
                       .setContentTitle(clickedinterval)
                       .setContentText("Pause" +  " Time remaining: " + minutesPauseCountDown + ":" + secondsPauseCountDown)
                       .build();

notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.DEFAULT_LIGHTS;

notificationManager.notify(0, notification);

1 个答案:

答案 0 :(得分:0)

in your case use android:launchMode="singleInstance" in mainfest file 
Or 
Intent.FLAG_ACTIVITY_NEW_TASK


<activity android:name=".Timer" android:label="@string/app_name"
                 android:launchMode="singleTop" />

也可以尝试

Intent intent = new Intent(Timer.this, Timer.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                PendingIntent pIntetn = PendingIntent.getActivity(timer.this, 0, intent, 0);

                notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                notification = new Notification.Builder(timer.this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentIntent(pIntetn)
                        .setContentTitle(clickedinterval)
                        .setContentText("Pause" +  " Time remaining: " + minutesPauseCountDown + ":" + secondsPauseCountDown)
                        .build();

                notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.DEFAULT_LIGHTS;

                notificationManager.notify(0, notification);