简体   繁体   English

带有按钮的通知,单击该按钮即可执行操作而不打开活动

[英]Notification with button that upon click take an action without opening activity

I am trying to create a notification where I add button to it that would basically do some action. 我正在尝试创建一个通知,在其中添加按钮,该通知基本上可以执行一些操作。 I know I can do the following 我知道我可以做到以下几点

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(con)
            .setSmallIcon(image)
            .setContentTitle("title")
            .addAction(icon, title, intent)

My questions are: 1) Is adding button supported in API 5.0+ ONLY or also in 4.x? 我的问题是:1)仅API 5.0+或4.x支持添加按钮吗? I read different answers about it 我对此有不同的回答

2) The action seems to be associated with opening an activity. 2)动作似乎与打开活动有关。 Is there away I can have it so when you click on a button it takes an action without having to open the activity (Either through broadcast receiver or some other way)? 有没有我可以拿到的东西,所以当您单击一个按钮时,它无需打开活动即可执行操作(通过广播接收器或其他方式)? As far as I know Intent opens activities. 据我所知,Intent打开活动。

Thank you so much 非常感谢

  1. It will work in Android 4.1 and later. 它将在Android 4.1及更高版本中运行。 See official doc . 参见官方文档

  2. If you want to do action with out any UI update(ie, showing any activity), I suggest send a pending intent(which will trigger a broadcast receiver) as a parameter for notification action 如果您要在不进行任何UI更新的情况下执行操作(即显示任何活动),建议发送一个挂起的意图(这将触发广播接收器)作为通知动作的参数

i) Create a BroadcastReceiver named MyBroadcastReceiver i)创建一个名为MyBroadcastReceiverBroadcastReceiver
ii) Add your action in BroadcastReceiver's onReceive method ii)在BroadcastReceiver's onReceive方法中添加您的操作
iii) Create a PendingIntent iii)创建一个PendingIntent

Intent mIntent = new Intent(this,MyBroadcastReceiver.class);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, mIntent , 0);

iv) Add it to Notification iv)将其添加到通知中

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(con)
        .setSmallIcon(image)
        .setContentTitle("title")
        .addAction(icon, title, mPendingIntent)

1) Is adding button supported in API 5.0+ ONLY or also in 4.x? 1)仅API 5.0+或4.x支持添加按钮吗?

It will work in Android 4.1 and later . 它将在Android 4.1及更高版本中运行 See official doc . 参见官方文档

2)...As far as I know Intent opens activities. 2)...据我所知,Intent打开活动。

Yes, you can use BroadcastReceiver or Service for executing logic that doesn't involve UI. 是的,您可以使用BroadcastReceiver或Service来执行不涉及UI的逻辑。 First of all, you can build intent to launch activity, broadcast receiver, or service. 首先,您可以建立启动活动,广播接收器或服务的意图。 Secondly, the third argument of NoticiationCompat.Builder#addAction is PendingIntent , not an Intent . 其次,NoticiationCompat.Builder#addAction的第三个参数是PendingIntent ,而不是Intent You can use PendingIntent.getService to create an PendingIntent for service, for instance. 例如,您可以使用PendingIntent.getService创建服务的PendingIntent。

http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context , int, android.content.Intent, int) http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context,int,android.content.Intent,int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM