我希望当用户将通知从屏幕上滑下或点击清除所有通知按钮时,应用程序进程将被终止。
有没有办法实现这个?
答案 0 :(得分:0)
DeleteIntent :DeleteIntent是一个PendingIntent对象,可以与通知关联,并在通知被删除时被触发。
您可以将Pending Intent设置为广播Receiver,然后执行您想要的任何操作。
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);
}
}