[英]Can I perform an action if a notification gets touched?
我看过一个演示手机在屏幕上方的通知被下拉时执行操作的演示。 通常,当您在Android上执行此操作时,通知菜单会打开。 它适用于所有应用程序,因此不仅适用于特定受支持的应用程序。 我曾尝试实现此功能,但找不到任何方法来执行此操作。
.addAction
只允许我添加按钮,但不允许这样。 理想情况下,我认为我需要在所有通知中实现类似onTouchEvent的东西。
下面的代码创建一个通知,如果您向它添加一个意图(在我的情况下为空),则在单击通知时可以执行一个操作。
public void buttonClick(View v){
final Intent emptyIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(MyActivitity.this, 0, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setAutoCancel(true) //when clicked it disappears
.setContentIntent(pendingIntent); //Required on Gingerbread and below
NotificationManager notificationManager = (NotificationManager) MyActivitity.this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.