2014-02-25 39 views
0

我使用以下代码将图标添加到通知状态,用户可以单击图标打开应用程序ui.SMSMain.class,并且两个ui.SMSMain.class应用程序将会如果用户点击两次图标,则会打开。 我希望应用程序只能打开一次,我该怎么办?如何防止应用程序打开重复

private static void ShowNotification() {   
     NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.smsforward, 
            myContext.getString(R.string.app_name), 
            System.currentTimeMillis()); 

    notification.flags |= Notification.FLAG_ONGOING_EVENT; 
    notification.flags |= Notification.FLAG_NO_CLEAR; 
    CharSequence contentTitle =myContext.getString(R.string.NotificationTitle); 
    CharSequence contentText = myContext.getString(R.string.NotificationContent); 

    Intent notificationIntent = new Intent(myContext, ui.SMSMain.class); 
    PendingIntent contentItent = PendingIntent.getActivity(myContext, 0, 
      notificationIntent, 0); 

    notification.setLatestEventInfo(myContext, contentTitle, contentText,contentItent); 
    notificationManager.notify(NotificationID, notification); 
} 
+0

您是否定义了目标活动的launch mide single_top? – Devrim

回答

1

附加launchMode属性的活动中表现为SingleTop

<activity 
     android:name="Tracking" 
     android:label="@string/app_name" 
     android:launchMode="singleTop"> 
     .... 

这将只允许一个活动的情况下,也最好是在你的PendingIntent

PendingIntent contentItent = PendingIntent.getActivity(myContext, 0, 
     notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
使用标志 PendingIntent.FLAG_UPDATE_CURRENT

虽然,我不确定是否0实际引用UPDATE_CURRENT

更新

您可能需要根据自己的需要singleTop或者singleTask中进行选择

为singleTop:如果活动的实例已经存在于当前任务和系统路线的意图本次活动的顶部,将不会创建新的实例

for singleTask:总是会创建一个新任务,并将新实例作为根目录推送到该任务。但是,如果任何活动实例存在于任何任务中,则系统通过onNewIntent()方法调用将意图路由到该活动实例。

更多关于LaunchMode的information

我相信singleTop更适合您的问题。

+1

听起来更像是他的描述中的一组任务的主要类,所以我可能甚至会考虑singleTask – Guardanis

+0

感谢分享,但是我认为从我所了解的他想要重定向到打开的活动而不启动新活动,所以我认为singleTop。 – Coderji

相关问题