2014-09-06 60 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启动模式,它会将此活动的任何现有实例重新排列到前面(因此它将从堆栈中的旧位置移除)。

告诉你的结构是否合适:)