Android Wear Intent来自通知未触发

时间:2016-10-27 18:32:07

标签: android android-intent wear-os android-wear-notification

我正在编写一个小型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();

注意:我尝试以另一种方式Intent tnsIntent = new Intent(ACTION_TERMINATE_TRACKING, null, this, TimerNotificationService.class);实例化tnsIntent,但它没有改变任何内容。

服务看起来像这样:

public class TimerNotificationService extends IntentService {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        clearSomeState();
    }
}

我在调试模式下运行应用程序并在服务的onHandleIntent()中放置一个断点,但我没有点击断点,因此服务甚至没有收到意图。我是否错过了一些意向登记电话,我应该在服务的某个地方做什么? (在清单中?)

1 个答案:

答案 0 :(得分:2)

如果tnsPendingIntent正在启动Service,则应根据您的代码段显示PendingIntent.getService,而不是PendingIntent.getActivity。 (reference