0

私は小さなAndroid Wear Appを作成しています。これは、次の2つのアクションで進行中の通知を作成することの一部です。 2.アプリケーションを閉じる(状態をクリアする)。Android Wear通知が送信されないという意図

最初のアクション(open)は完全に機能します(つまり、ボタンを押すとアクションが実行されます)。しかし、第二の行動は何も発射しない。

// 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()にブレークポイントを入れて、私は、ブレークポイントにヒットしなかったので、サービスはしても受信しません意図。私がサービスのためにどこかでやっていなければならない意図的な登録コールがありませんでしたか? (マニフェストで?)

答えて

2

tnsPendingIntentServiceを起動している場合、それはあなたのコードスニペットが示すようにPendingIntent.getService、ないPendingIntent.getActivityから設定する必要があります。 (reference

+0

良いキャッチ!それは確かに問題の1つでした。 しかし、主な問題は、(着用)アプリケーションの「AndroidManifest.xml」でサービスを宣言しなければならないことでした。 _ "すべてのサービスは、マニフェストファイル内の要素で表されなければならない。宣言されていないものはシステムには見られず、決して実行されないだろう" _([reference] (https://developer.android.com/guide/topics/manifest/service-element.html) 答えに含めることができれば、それは完璧です。ありがとう! – Undo

+0

私は実際それについて言及したと考えましたが、あなたのマニフェストを投稿していないので、私はそれを批判することが予想されると判断しました。 :^)あなたはそれを整理してうれしい! – String

関連する問題