在Android中如何重新启动App On Click Application" onGoing"通知。如果App是开放的

时间:2015-07-14 13:57:09

标签: android eclipse android-studio notifications android-service

在Android中如何重启App on Click Application" onGoing"通知。如果App是开放的。

喜欢当我点击进行通知"连接为媒体设备"

1 个答案:

答案 0 :(得分:0)

您可以通过添加PendingIntent来定义与通知进行交互时要执行的操作。

在以下示例中,创建PendingIntent以启动(当前)活动。 然后将该意图添加到内容部分中的通知中。显示此通知后,当您单击内容部分时,会触发意图并启动应用程序或将其重新置于顶部。

private static final int NOTIFICATION_ID = 1;
...
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
        0);
nb.setSmallIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha) 
    .setCategory(NotificationCompat.CATEGORY_STATUS) 
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .setContentTitle(getText(R.string.app_name))
    .setContentText("Click to launch!")
    .setWhen(System.currentTimeMillis())
    .setContentIntent(pendingIntent) // Here the "onClick" Intent is added
    .setOngoing(false);

nm.notify(NOTIFICATION_ID, nb.build());

在这种情况下,通知是不允许的。如果设置.setOngoing(true),则需要通过在NotificationManager的实例上调用.cancel(NOTIFICATION_ID)来删除它。

另见介绍如何Build a Notification