当用户点击通知时,如何停止或退出应用程序?

时间:2017-03-26 12:49:29

标签: android notifications

当用户按下通知图标时,我想停止或退出我的应用程序,这是我的代码。使用此代码,我可以通过按通知图标打开我的主要活动。我想要的是有人改变我现有的代码,请帮助我

  NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
  builder.setSmallIcon(R.drawable.ic);
  builder.setContentTitle("RadioPlanet");
    builder.setContentText("Touch here to stop");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());

我的包名是com.hackerinside.jaisonjoseph.radioplanet

1 个答案:

答案 0 :(得分:0)

不太熟悉通知,但这是一个建议:

创建新活动。对于此示例,我将其称为MainActivityStop。从通知中,将PendingIntent发送到MainActivityStop。在MainActivityStop中,创建一个新的intent和putExtra。例如:

Intent i = new Intent(MainActivityStop.this, MainActivity.class);
i.putExtra("stop", true);
startActivity(i);

收到它:

boolean stop = false;

try{
    stop = getIntent().getBooleanExtra("stop", false);
}catch(Exception e){
//ignore this. If it gets here, the boolean cannot be found. It may never go to the catch block, it is a safety precaution
}

(...) Do whatever you do to stop or exit.