2014-09-06 57 views
0

我正在嘗試將特殊操作添加到通知中,以便我可以使用智能手錶來遠程控制應用程序。智能手錶通知及行動

這是我當前的代碼:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setContentTitle("Control Notification"); 
builder.setContentText("With this notification I should be able to control the app with my watch."); 
builder.setSmallIcon(R.drawable.ic_launcher); 
Intent actionIntent = new Intent(this, MainActivity.class); 
actionIntent.putExtra("action", true); 
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
builder.addAction(R.drawable.ic_launcher, "action", resultPendingIntent); 
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(0, builder.build()); 

的問題是,每次我火我的手錶一個新的活動打開了我的手機上的動作。但我想重複使用舊的活動。我需要改變什麼?順便說一下:如果您有任何其他關於我的代碼的提示或改進,請隨時告訴我。

回答

0

實現它的最簡單方法是在AndroidManifest.xml文件中聲明您的活動爲singleInstance。您需要在相關的活動代碼中添加android:launchMode="singleInstance"

但它確實取決於你想要達到什麼目的。使用singleInstance啓動模式,它會將此活動的任何現有實例重新排列到前面(因此它將從堆棧中的舊位置移除)。

告訴你的結構是否合適:)