При нажатии на уведомление запускается действие дважды

Я создаю уведомление от службы со следующим кодом:

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)

Почему они запускаются дважды?

Есть два идентичных вопроса по stackoverflow: здесь и здесь . Но они не объясняют, в чем может быть первоначальная проблема, и для меня они не работают. Например, переход на launchMode singleTop мне не подходит, и он должен работать без изменения launchMode в соответствии с официальными документами (см. Вызов диалогового окна поиска).

Тем не менее, я также попытался добавить следующие флаги в notificationIntent

Intent.FLAG_ACTIVITY_CLEAR_TOP | PendingIntent.FLAG_UPDATE_CURRENT

, но проблема остается прежней.

11
задан Community 23 May 2017 в 11:55
поделиться