2017-01-26 55 views
0

我創建了一個通知,我想在點擊通知後繼續應用。現在當我點擊通知時,通知消失了,我想恢復我的應用程序(當最小化時)。用Java Android單擊通知後如何恢復應用程序?

public static void getSynchronizeNotification(Context context, DataResponse dataResponse) { 

     Bitmap Largeicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); 
     KeyguardManager km = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE); 
     final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN"); 
     kl.disableKeyguard(); 

     PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "SMOK Komunal"); 
     wl.acquire(); 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context) 
       .setCategory(Notification.CATEGORY_PROMO) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Zmiany po synchronizacji...") 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse))) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setLargeIcon(Largeicon) 
       .setContentIntent(pendingIntent) 
       .setVibrate(vibratePattern) 
       .setLights(Color.GREEN, 2000, 2000) 
       .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); 
     notificationManager.notify(0, notificationBuilder.build()); 
//  wl.release(); 
    } 

回答

0

我做到這一點,它的工作原理:

在MainAcitivity我:

public static Context currentContext; 

接着,在的onCreate和的onResume所有活動,並做這個:

MainActivity.currentContext = this; 

而接下來當我建立一個通知我這樣做:

Intent intent = new Intent(context, MainActivity.currentContext.getClass()); 
     intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT); 
0

編輯這一行

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); 
+0

我不想要移動到MainActivity我想繼續我的應用程序(開放應用程序中的活動,我是) –

+0

,當我使用這將不會完成舊的活動,當你在MainActivity,並點擊一個通知是打開MainActiviti當你按回它將關閉MainActivity(打開通知),並恢復MainActivity –

+0

http://stackoverflow.com/questions/12043671 /通知單擊活動 - 已打開 –