2017-09-21 6 views
0

私はstackoverflowに多くの投稿を参照していますが、私の問題の修正が見つかりません。リモートビューの保留中の意図が機能していません

私はRemoteViewsにリンクされたボタン付きのカスタムコンテンツビューで通知しています。私はこのリンク(Adding button action in custom notification)を使ってボタンにアクションを添付しましたが、私のBroadcastReceiverは決して解雇されません。コード:

private void createNotification(int index){ 
    final int id = index; 
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
    contentView.setInt(R.id.container,"setBackgroundColor",ContextCompat.getColor(this,BG_COLORS[index])); 
    contentView.setImageViewResource(R.id.logo,BUTTON_OFF[index]); 
    contentView.setTextViewText(R.id.sound, getString(SOUND_NAME[index])); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContent(contentView); 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, id, notificationIntent, 0); 

    Notification notification = mBuilder.build(); 
    notification.contentIntent = contentIntent; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    Intent closeButton = new Intent(this,StopReceiver.class); 
    closeButton.setAction(StopReceiver); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, closeButton,0); 
    contentView.setOnClickPendingIntent(R.id.stop,pendingIntent); 


    mNotificationManager.notify(id, notification); 


} 

    public static class StopReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
      //Never called 
     Log.d(LOGTAG,"Received Cancelled Event"); 
     ... 
    } 
} 

は、私も自分のAndroidManifest.xmlで私の受信機を宣言している:

<receiver android:name="StopReceiver" android:enabled="true" /> 

ありがとうございました。

答えて

0

RemoteViewsのsetOnClickPendingIntentは、通知のカスタムRemoteViewとして提供した後で設定します。

setContentを呼び出す前にsetOnClickPendingIntentを設定してみてください。例:

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
contentView.setInt(R.id.container,"setBackgroundColor",ContextCompat.getColor(this,BG_COLORS[index])); 
contentView.setImageViewResource(R.id.logo,BUTTON_OFF[index]); 
contentView.setTextViewText(R.id.sound, getString(SOUND_NAME[index])); 

Intent closeButton = new Intent(this,StopReceiver.class); 
closeButton.setAction(StopReceiver); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, closeButton,0); 
contentView.setOnClickPendingIntent(R.id.stop,pendingIntent); 

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContent(contentView); 
関連する問題