2013-10-03 169 views
7

我已經創建了一個管理短信的應用程序,我創建了通知,但是當我點擊它們開始另一個活動時,我想知道如何檢查活動是否有已被停止並恢復。當點擊通知時恢復活動

下面是用於創建的PendingIntent代碼:

private void createNotification(SmsMessage sms, Context context){ 

    final NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

    String contentTitle = ""; 


    // construct the Notification object. 
     final NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
     .setContentTitle(contentTitle) 
     .setContentText(sms.getMessageBody()) 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setLargeIcon(getIconBitmap()) 
     .setNumber(nmessages); 

     builder.setAutoCancel(true); 

     //(R.drawable.stat_sample, tickerText, 
      //  System.currentTimeMillis()); 

     // Set the info for the views that show in the notification panel. 
     //notif.setLatestEventInfo(this, from, message, contentIntent); 
     /* 
     // On tablets, the ticker shows the sender, the first line of the message, 
     // the photo of the person and the app icon. For our sample, we just show 
     // the same icon twice. If there is no sender, just pass an array of 1 Bitmap. 
     notif.tickerTitle = from; 
     notif.tickerSubtitle = message; 
     notif.tickerIcons = new Bitmap[2]; 
     notif.tickerIcons[0] = getIconBitmap();; 
     notif.tickerIcons[1] = getIconBitmap();; 
     */ 

    // Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(context, BasicActivity.class); 

     resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     // Because clicking the notification opens a new ("special") activity, there's 
     // no need to create an artificial back stack. 
     PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
      context, 
      0, 
      resultIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT 
     ); 


     // Ritardo in millisecondi 



    builder.setContentIntent(resultPendingIntent); 

    nm.notify(R.drawable.ic_drawer, builder.build()); 
+0

顯示你的意圖/的PendingIntent的代碼。 –

+1

請更詳細地解釋發生了什麼以及你想要發生什麼。你的問題不清楚。 –

+0

我解決它與此:http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather-than-making-a-new-intent/39482464#39482464 – TharakaNirmana

回答

7

你需要設置你的PendingIntent的標誌......像FLAG_UPDATE_CURRENT。

這裏就是它的全部。 http://developer.android.com/reference/android/app/PendingIntent.html

編輯1:我誤解了這個問題。

以下是主題的鏈接有同樣的問題,但都解決了:

resuming an activity from a notification

Notification Resume Activity

Intent to resume a previously paused activity (Called from a Notification)

Android: resume app from previous position

請仔細閱讀上面的答案一個完整的解決方案,讓我知道它是否工作秒。

+0

已完成,它不工作! – codareee

+0

已更新答案 – JanBo

2

做了很多搜索後實際工作對我來說,唯一的解決辦法是做到以下幾點:

這裏你只需啓動應用程序的保持當前堆棧:

//here you specify the notification properties 
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).set...(...).set...(..); 

//specifying an action and its category to be triggered once clicked on the notification 
Intent resultIntent = new Intent(this, MainClass.class); 
resultIntent.setAction("android.intent.action.MAIN"); 
resultIntent.addCategory("android.intent.category.LAUNCHER"); 

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

//building the notification 
builder.setContentIntent(resultPendingIntent); 
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(0, builder.build()); 
+0

確實解釋了一些有關您的解決方案如何工作以及OP方法學有什麼問題。 –

2

嘗試用這個。

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        mContext).setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle(mContext.getString(R.string.notif_title)) 
        .setContentText(mContext.getString(R.string.notif_msg)); 
      mBuilder.setAutoCancel(true); 

     // Set notification sound 
     Uri alarmSound = RingtoneManager 
       .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     mBuilder.setSound(alarmSound); 

     Intent resultIntent = mActivity.getIntent(); 
     resultIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
     resultIntent.setAction(Intent.ACTION_MAIN); 

     PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(pendingIntent); 
     NotificationManager mNotificationManager = (NotificationManager) mContext 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     // mId allows you to update the notification later on. 
     mNotificationManager.notify(mId, mBuilder.build()); 
+0

這也可以正常工作 意圖resultIntent = new Intent(mContext,ActivityWebView.class); resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); – Thushara

0

將此行添加到您的應用的清單文件中的相應活動。

機器人:launchMode = 「singleTask」

如:

<activity 
android:name=".Main_Activity" 
android:label="@string/title_main_activity" 
android:theme="@style/AppTheme.NoActionBar" 
android:launchMode="singleTask" />