java - 通知点击事件而不显示 Activity ?

标签 java android android-notifications

我想分配一个在点击通知时触发的事件,而不显示 Activity (如解释的 here )。

我怎样才能做到这一点?

最佳答案

您可以通过注册广播接收器来做到这一点。这样,您可以在单击通知时在接收器中运行代码,而无需显示任何 UI(如果您愿意)。

基本接收器代码:

public class Provider extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        //Your code here

    }
}

在 list 中:

<receiver android:name="Provider" />

通知的示例代码:

public static void showNotification(Context context) {
    CharSequence title = "title";
    CharSequence message = "text";

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(<image>, title, System.currentTimeMillis());
    notification.flags = Notification.FLAG_ONGOING_EVENT;

    Intent notificationIntent = new Intent(context, Provider.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, pendingIntent);
    notificationManager.notify(<notification id>, notification);
}

关于java - 通知点击事件而不显示 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14416402/

相关文章:

java - 如何通过子类访问父类变量

使用 Eclipse 和 Jetty 的 Java RESTful Web 服务教程

php - android、json、php、mysql中保存并获取汉字数据。

android - 带灰色方 block 的通知背景

android - NotificationListenerService 停止并且不重启就无法重启

java - -4偏移后当前时间为负

java - 画面转换

java - 如何在我的文档中记录或排除生成的 BuildConfig 类?

Android FCM通知分组

java - JBoss 在 Windows 上运行良好吗?