2013-08-27 32 views
1

我想通过使用捆绑通过一个待处理意图从我的活动从服务传递一些值。如果应用程序刚刚启动,则一切正常,但当应用程序处于恢复模式时,尽管我的服务从远程服务器接收到新值并将其置于待传递给活动,但该活动显示旧值。下面是服务端的代码:Android:无法从重新启动服务替换捆绑

private void sendNotification(String wholemsg) { 
    mNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 



    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.setAction(Intent.ACTION_MAIN); 
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

    /* Do something to extract salesID and notificationmessage from wholemsg 
    .... 
    .... 
    .... 
    salesID=.... 
    notificationmessage=... 
    .... 
    */ 


    Bundle bundle=new Bundle(); 
    bundle.putString("msg", notificationmessage); 
    bundle.putString("strsalesID", salesID);  

    notificationIntent.replaceExtras(bundle); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent,  PendingIntent.FLAG_ONE_SHOT+PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
      this) 

    .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("AppV001 Notification") 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationmessage)) 
      .setContentText(notificationmessage); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

} 

这是对我的活动onRestart方法:

  @Override 
     protected void onRestart() { 

    super.onRestart(); 


    NotificationManager mNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.cancel(1); 
    try { 

     Intent intent = getIntent(); 
     if (intent.hasExtra("msg") && intent.hasExtra("strsalesID")) { 
      String strmsgtitle = intent.getStringExtra("msg"); 
      salesID = intent.getStringExtra("strsalesID"); 
      titletext.setText(getString(R.string.str_ettitle) + salesID); 
     } 
    } catch (Exception ex) { 
     return; 
    } 

} 

的问题是,salesID保持之前的值时,应用程序来自隐藏模式回来。看起来,该服务在隐藏时无法更改活动包。

非常感谢您的时间!

+0

根据什么launchMode你已经在你的清单这项活动的规定,你可能。需要重写['onNewIntent()'](http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent))以从服务接收更新的'Intent' – acj

+0

感谢您回复!我尝试使用onNewIntent()但它不会触发事件。这里是清单: user2723342

回答

0

我发现我的代码和我想发布在这里以防万一别人面临同样的问题时出了什么问题。 acj是对的,我的启动模式有问题。我的原始应用程序清单是这样的:

<activity 
     android:name="com.example.appv001.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

由于没有声明启动模式,所以标准模式是自动进行的。我们可以有四个launchMode (look at this)

'标准' 为默认值。这四个值分为两个组 :

  • “标准”和“singleTop”可以实例多个活动实例,实例将留在相同的任务。
  • 对于'singleTask'或'singleInstance',活动类使用单例模式,该实例将成为新任务的根活动。

由于我的活动有标准发射模式下,当服务被调用它的活动的一个新实例被创建和意图传递给这一新的活动。因此,不是调用“onNewIntent()”,而是调用“onCreate”方法,换句话说,“onNewIntent()”从未被调用来设置包等。我将活动清单更改为此,并设置启动模式到singleTask(机器人:launchMode =“singleTask),只是解决了这个问题:

<activity 
     android:name="com.example.appv001.MainActivity" 
     android:label="@string/app_name" 
     android:launchMode="singleTask"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

我希望这可以帮助别人