2011-04-04 121 views
11

我创建从服务用下面的代码的通知:点击通知开始活动两次

NotificationManager notificationManager = (NotificationManager) ctx 
.getSystemService(Context.NOTIFICATION_SERVICE); 

CharSequence tickerText = "bla ..."; 
long when = System.currentTimeMillis(); 
Notification notification = new Notification(R.drawable.icon, 
tickerText, when); 

Intent notificationIntent = new Intent(ctx, SearchActivity.class). 
putExtra(SearchActivity.INTENT_SOURCE, 
MyNotificationService.class.getSimpleName()); 

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, 
notificationIntent, 0); 
notification.setLatestEventInfo(ctx, ctx.getString(R.string.app_name), 
tickerText, contentIntent); 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 
notificationManager.notify(1, notification); 

日志清楚地说,该方法startActivity被调用的两倍:

04-02 23:48:06.923: INFO/ActivityManager(2466): Starting activity: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,520][480,616] (has extras) } 
04-02 23:48:06.923: WARN/ActivityManager(2466): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,520][480,616] (has extras) } 
04-02 23:48:06.958: INFO/ActivityManager(2466): Starting activity: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,0][480,96] (has extras) } 
04-02 23:48:06.958: WARN/ActivityManager(2466): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,0][480,96] (has extras) } 
04-02 23:48:07.087: INFO/notification(5028): onStartCmd: received start id 2: Intent { cmp=com.xy/.NotificationService } 
04-02 23:48:07.310: INFO/notification(5028): onStartCmd: received start id 3: Intent { cmp=com.xy/.NotificationService } 
04-02 23:48:07.392: INFO/ActivityManager(2466): Displayed activity com.xy/.SearchActivity: 462 ms (total 462 ms) 
04-02 23:48:07.392: INFO/ActivityManager(2466): Displayed activity com.xy/.SearchActivity: 318 ms (total 318 ms) 

他们为什么开始两次?

在计算器上有两个相同的问题:herehere。 但他们没有解释最初的问题可能是什么,他们不适合我。例如。更改为launchMode singleTop不适用于我,它应该根据official docs(请参阅调用搜索对话框)在不更改launchMode的情况下工作。

不过我也试过以下标志添加到notificationIntent

Intent.FLAG_ACTIVITY_CLEAR_TOP | PendingIntent.FLAG_UPDATE_CURRENT

但问题依然存在。

+0

我从头开始创建一个测试项目,同样的问题存在那里(在三星设备上)。对于模拟器(2.1 + 2.2)似乎都很好。 – Karussell 2011-04-05 10:03:24

回答

14

同样在这里,三星Galaxy S上出现问题。任何一种解决方法的好主意?

我现在正在检查是否已经收到类似的Intent并且如果是的话就完成这个活动。它可以工作,但看起来不太好。

我们可以以某种方式取消第二个意图吗?

UPDATE:我可以通过设置FLAG_ONE_SHOT这样防止问题:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 
+0

不错!感谢您确认这一点。这使我几乎疯狂:)我会更新我的解决方案的答案 – Karussell 2011-04-08 09:42:40

0

这似乎是一个三星银河系统的bug。确认在其他设备上一切正常。

(也no discussion at google groups见)

一个解决办法可能是取消第二意图的笨拙,但工作诀窍(不知道这对其他设备的副作用):

// put a hint into the notification and check if the intent 
// comes from notification or not: 
String intentSource = queryIntent.getStringExtra(INTENT_SOURCE); 
if (NotificationService.class.getSimpleName().equals(intentSource)) { 
// don't do this again e.g. when rotating! 
queryIntent.removeExtra(INTENT_SOURCE); 

if (getIntent() != null && getIntent().getSourceBounds() != null) { 
    if (getIntent().getSourceBounds().top == 0 
      && getIntent().getSourceBounds().left == 0) { 
     Log.w("search", "Problematic double trigger!"); 
    } 
} 
} 

所以使用这要小心,并可能检查是否在前一秒完成了前一次点击,那很可能是双触发问题。

1

调用它释放键或按下键(一般避免ACTION)!

if(event.getAction() == event.ACTION_UP){ 
    Intent intent = new Intent(......); 
    startActivityForResult(intent, 0); 
} 
5

请使用下列标志。

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
          |Intent.FLAG_ACTIVITY_SINGLE_TOP); 
+3

这不适用于** PendingIntent ** – swiftBoy 2013-12-16 09:00:36