2013-10-03 137 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" />