如何创建通知,点击后关闭应用程序?

时间:2017-12-28 22:16:06

标签: java android notifications

如何创建简单的通知,点击后会关闭应用程序(多任务处理)?谢谢。 (Java,Android)

1 个答案:

答案 0 :(得分:0)

首先我们应该创建一个调用活动的通知:

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);
}

然后在你班级的 onCreate()中执行以下操作:

Bundle extras = getIntent().getExtras();

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

对于完全关闭的应用程序,您可以终止进程:

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