0

我現在正在處理通知提醒,如果用戶點擊通知,它將打開應用程序。在Android中點擊通知時避免重新創建活動

但是,問題是,如果我已經打開應用程序,當我點擊通知時,它仍然會創建一個新的活動,所以總共有兩個活動。

我該如何做到這一點?如果用戶已經打開該應用程序,則不做任何操作,如果沒有,則打開該應用程序。感謝您的幫助。

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); 
    } 

} 
+0

我從來沒有見過,雖然用戶如果用戶使用的應用程序中使用它,即創建通知的應用程序,通知他低谷的應用程序本身。 – Marius

回答

1

請嘗試以下代碼。

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); 
     } 

    } 

完蛋了......