【问题标题】:In Android How to Restart App On Click Application "onGoing" Notification. if App is open or not在 Android 如何在单击应用程序“onGoing”通知时重新启动应用程序。 App是否打开
【发布时间】:2015-07-14 13:57:08
【问题描述】:

在 Android 中如何在单击应用程序“onGoing”通知时重新启动应用程序。 App是否打开。

就像当我点击“作为媒体设备连接”的 onGoing 通知时一样

【问题讨论】:

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


    【解决方案1】:

    您可以通过添加PendingIntent 来定义您希望在与通知交互时发生的操作。

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

    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,另请参阅此介绍。

    【讨论】:

      猜你喜欢
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多