Android Wear Intent из уведомлений не стреляет
Я пишу небольшое приложение для Android Wear, и частично оно создает постоянное уведомление с двумя действиями: 1. Снова откройте приложение. 2. Закройте (очистите состояние) приложения.
Первое действие (открыть) работает отлично (т.е.: я нажимаю кнопку, действие выполняется). Но второе действие ничего не увольняет.
Вот код самого уведомления:
// First action: open app's main activity
Intent actionIntent = new Intent(this, MainActivity.class);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.close_button,
getString(R.string.open_app), actionPendingIntent)
.build();
// Second action: clear the state of the app, by firing a service which will do so
Intent tnsIntent = new Intent(this, TimerNotificationService.class);
PendingIntent tnsPendingIntent =
PendingIntent.getActivity(this, 0, tnsIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Action closeAppAction =
new NotificationCompat.Action.Builder(R.drawable.close_button,
getString(R.string.close_app), tnsPendingIntent)
.build();
return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.common_signin_btn_icon_dark)
.setContentTitle(getString(R.string.time_tracking_on))
.setContentText("Tap to open app")
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.extend(new NotificationCompat.WearableExtender().addAction(action).addAction(closeAppAction))
.setLocalOnly(true)
.build();
Примечание. Я попытался создать экземпляр tnsIntent другим способом. Intent tnsIntent = new Intent(ACTION_TERMINATE_TRACKING, null, this, TimerNotificationService.class);
но это ничего не изменило.
Сервис выглядит так:
public class TimerNotificationService extends IntentService {
@Override
public void onCreate() {
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
clearSomeState();
}
}
Я запустил приложение в режиме отладки и установил точку останова в onHandleIntent()
службы и я не достиг точки останова, поэтому служба даже не получает намерения. Я пропустил какой-нибудь звонок о регистрации намерения, который я должен сделать где-нибудь для службы, возможно? (в манифесте?)
1 ответ
Если tnsPendingIntent
запускает Service
должно быть установлено от PendingIntent.getService
не PendingIntent.getActivity
как показывает ваш фрагмент кода. ( ссылка)