【问题标题】:Stop Process when Notification is Cleared清除通知时停止进程
【发布时间】:2016-09-09 13:52:57
【问题描述】:

我希望在用户将通知滑出屏幕或点击清除所有通知按钮时终止应用程序进程。

有没有办法实现这个?

【问题讨论】:

标签: android android-notifications


【解决方案1】:

DeleteIntent:DeleteIntent 是一个 PendingIntent 对象,可以与通知关联并在通知被删除时触发。

您可以将 Pending Intent 设置为广播接收器,然后执行您想要的任何操作。

Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
  Builder builder = new Notification.Builder(this):
 ..... code for your notification
  builder.setDeleteIntent(pendingIntent);

MyBroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
         //code to kill app
         c̶o̶n̶t̶e̶x̶t̶.̶f̶i̶n̶i̶s̶h̶A̶f̶f̶i̶n̶i̶t̶y̶(̶)̶;̶
         ActivityCompat.finishAffinity((Activity)context);
     }
}

参考:catch on swipe notification event

【讨论】:

  • finishAffinity()在哪里定义,接收方不解析方法。我也尝试了here 给出的替代方案,但它们都不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多