私は小さな Android Wear アプリを作成しています。その機能の一部として、2 つのアクションを含む進行中の通知を作成しています。1. アプリを再度開きます。2. アプリを閉じる(状態をクリアする)。
最初のアクション (開く) は完全に機能します (つまり、ボタンを押すと、アクションが実行されます)。しかし、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()
、サービスにブレークポイントを設定しましたが、ブレークポイントにヒットしなかったため、サービスはインテントを受け取りません。おそらくサービスのためにどこかで行うべきであるインテント登録呼び出しを見逃しましたか? (マニフェストで?)