Android 如何通过单击通知中的按钮关闭应用程序?

Android 如何通过单击通知中的按钮关闭应用程序?,android,Android,我尝试在单击按钮后关闭我的应用程序 我知道我需要使用pending intent和intent来实现这一点,但我不知道如何实现,以及可以使用哪些标志或操作 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); NotificationCompat.Builder builder = new NotificationCompat.Builder(t

我尝试在单击按钮后关闭我的应用程序

我知道我需要使用pending intent和intent来实现这一点,但我不知道如何实现,以及可以使用哪些标志或操作

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "")
            .setSmallIcon(R.mipmap.ic_launcher_notification)
            .setContentTitle("Close your app")
            .setContentText("Your app is still running...")
            .addAction(R.drawable.ic_exit,"Close App",
                    PendingIntent.getBroadcast(this, 0, intent, 0))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, builder.build());

这不起作用…

您想强制停止应用程序或仅完成活动关闭Main活动已足够,然后仅完成活动