我想通过通知停止主要活动

时间:2017-04-24 04:19:55

标签: android push-notification

我有一个Android应用程序,它将播放在线广播,甚至用户最小化应用程序。我想要做的是,当用户按下通知时,它应该停止或销毁我的主要活动,我有通过通知打开主要活动的代码,它工作正常,我想按照mu更改代码我只需要帮助......

  NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
  builder.setSmallIcon(R.drawable.ic);
  builder.setContentTitle("RadioPlanet");
    builder.setContentText("Touch here to Open");int mll=001;
   PendingIntent contentIntent =
            PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

    builder.setContentIntent(contentIntent);
  NotificationManager mNotification=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  mNotification.notify(mll,builder.build());

在oncreate中添加答案后

 BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }

    };


    IntentFilter filter = new IntentFilter("android.intent.CLOSE_ACTIVITY");
    registerReceiver(mReceiver, filter);






  NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
  builder.setSmallIcon(R.drawable.ic);
  builder.setContentTitle("RadioPlanet");
    builder.setContentText("Touch here to Open");int mll=001;
    Intent intent = new Intent("android.intent.CLOSE_ACTIVITY");


    Context context=getApplicationContext();

    PendingIntent pIntent = PendingIntent.getBroadcast(context, 0 , intent, 0);


    builder.setContentIntent(pIntent);


  NotificationManager mNotification=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  mNotification.notify(mll,builder.build());

1 个答案:

答案 0 :(得分:1)

尝试使用广播接收器。

IntentFilter filter = new IntentFilter("android.intent.CLOSE_ACTIVITY");
registerReceiver(mReceiver, filter);

然后在活动的onCreate()

Intent intent = new Intent("android.intent.CLOSE_ACTIVITY");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0 , intent, 0);

然后你的通知中的PendingIntent应该有“android.intent.CLOSE_ACTIVITY”的动作,为了安全起见你的活动包的包

作为

set number

然后在使用Notification.Builder构建通知时,使用setContentIntent(pIntent)将其添加到通知中。