Android 处理按钮点击通知

Android 处理按钮点击通知,android,notifications,onclicklistener,Android,Notifications,Onclicklistener,我收到了一个带有一个按钮的通知,当我点击通知按钮时,我想做点什么 有没有像OnClickListener这样的可能性来处理这个问题 以下是通知的代码: private void notification_anzeigen(){ Intent intent = new Intent(this, GestureAnyWhere.class); // String notificationMessage = "GestureAnyWhere läuft im Hintergru

我收到了一个带有一个按钮的通知,当我点击通知按钮时,我想做点什么

有没有像
OnClickListener
这样的可能性来处理这个问题

以下是通知的代码:

 private void notification_anzeigen(){


    Intent intent = new Intent(this, GestureAnyWhere.class);

    // String notificationMessage = "GestureAnyWhere läuft im Hintergrund";

    // intent.putExtra("NotificationMessage", notificationMessage);
    // intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);


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

    // build notification
    // the addAction re-use the same intent to keep the example short
    Notification n  = new Notification.Builder(this)
            .setContentTitle("GestureAnyWhere läuft im Hintergrund")
            //.setContentText("GestureAnyWhere muss im Hintergrund ausgeführt werden, um Gesten zu erkennen")
            .setSmallIcon(R.drawable.ic_launcher)       // muss rein, ansonsten wird keine Notifikation angezeigt
            .setContentIntent(pIntent)
            .addAction(R.drawable.ic_launcher, "Plus-Button", pIntent)
            .setStyle(new Notification.BigTextStyle().bigText("Auf den Plus-Button drücken, um auf aktueller Seite Gesten zu erkennen"))
            .setAutoCancel(true).build();

    n.setLatestEventInfo(getApplicationContext(), "Plus-Button", "neu", pIntent);


    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, n);
}
对不起,问题不清楚。所以我试着把它具体化一点:

  • 我有一个后台服务,当我最小化我的应用程序时启动
  • 当服务启动时,将显示一个新的通知,通知用户应用程序现在在后台工作
  • 后台服务应存在,以便检测应用程序外部绘制的手势
  • 当我点击通知并且应用程序再次启动时,服务将被停止(否则我可以在应用程序内部绘制,不再点击任何东西)->所以这是我想做的第一点:当我点击通知时,停止后台服务
  • 当我模拟我的应用程序并启动服务时,我现在只能在主屏幕上绘制->这是我想做的第二点:当用户单击通知下的按钮时,将调用一个活动,以便用户可以在当前视图上绘制
因为我只想知道在一般情况下如何处理点击通知(或通知下的按钮),所以我不提供服务和所有其他内容的代码

我希望这个问题和我想做的事情更清楚一点


非常感谢

嗯,据我所知,您希望在恢复活动后立即停止服务。 为了做到这一点,你们可以在PendingEvent的内在意图中添加一些额外的东西。
从活动中的意图解析此额外内容(如有必要,请使用onCreate和onNewIntent),然后调用Context.stopService()

,您到底想做什么?您可以广播您的意图并在
广播接收器中处理它
,也可以在
活动
中附加一个额外的意图并检查它。问题不清楚,请编辑您的问题您到底想做什么???抱歉。这次我试图更好地解释这个问题