简体   繁体   English

如何创建在单击后关闭应用程序的通知?

[英]How can I create notification which will close the app after clicking?

How can I create simple notification, which will close the application (in multitasking) after click?如何创建简单的通知,单击后将关闭应用程序(在多任务处理中)? Thanks.谢谢。 (Java, Android) (Java、安卓)

first we should create notification that call an activity :首先,我们应该创建调用活动的通知:

Intent intent = new Intent(this, YourClass.class);
intent.putExtra("NotiClick",true);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
    Notification Noti;
    Noti = new Notification.Builder(this)
            .setContentTitle("YourTitle")
            .setContentText("YourDescription")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pIntent)
            .setAutoCancel(true).build();

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(0, Noti);
}

Then in the onCreate() of your class do this:然后在您班级的onCreate()中执行以下操作:

Bundle extras = getIntent().getExtras();

if(extras == null) 
{
    //not being clicked on
} 
else if (extras.getBoolean("NotiClick"))
{
    //being clicked
}

and for completely close application you can kill process :对于完全关闭的应用程序,您可以终止进程:

android.os.Process.killProcess(android.os.Process.myPid());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM