如何按下通知文本关闭应用程序不打开其他活动

时间:2016-05-03 06:34:15

标签: android notifications flashlight

我正在开发一个flashLight应用程序,我在通知中遇到了一个问题:当我按下通知文本时我想要关闭闪存并关闭整个应用程序,我怎样才能修改通知方法来做到这一点? 我试图制作anthor活动并在其中放置和关闭app方法,但它不起作用。 请提前帮助和感谢。

这是我的通知方法

public void getNotification()
{
    Intent intent = new Intent();
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContentIntent(pIntent)
            .setContentTitle(res.getString(R.string.notification_title))
            .setContentText(res.getString(R.string.notification_text))
            .setSmallIcon(R.drawable.icon)
            .setAutoCancel(true)
            .setTicker(getString(R.string.notification_ticker_msg));
    // Build the notification:
    Notification notification = builder.build();

    // Get the notification manager:
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    // Publish the notification:
    final int notificationId = 0;
    notificationManager.notify(notificationId, notification);
}

3 个答案:

答案 0 :(得分:1)

如果您使用相同的Activity,则会再次调用onCreate方法。您可以使用extra发送一个Intent,表示通过点击通知生成Intent。在Activity onCreate中,检查此额外内容并致电finish()(如果有)。

public void getNotification() {
    Intent intent = new Intent(this, FlashActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra("origin", "notification");

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContentIntent(pIntent)
            .setContentTitle(res.getString(R.string.notification_title))
            .setContentText(res.getString(R.string.notification_text))
            .setSmallIcon(R.drawable.icon)
            .setAutoCancel(true)
            .setTicker(getString(R.string.notification_ticker_msg));
    // Build the notification:
    Notification notification = builder.build();

    // Get the notification manager:
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    // Publish the notification:
    final int notificationId = 0;
    notificationManager.notify(notificationId, notification);
}

onCreate的{​​{1}}方法中查看额外的内容。

FlashActivity

答案 1 :(得分:0)

我相信你可以在按下通知时在你的Activity上使用finish()。

编辑:How to close any activity of my application by clicking on a notification?

答案 2 :(得分:0)

您想使用PendingIntent。

show d

show tables更新中,具体取决于您的代码

show t