处理android通知中的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12438209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:40:02  来源:igfitidea点击:

Handling buttons inside android notifications

androidbuttonnotificationsstatus

提问by A E

I added a button inside a notification

我在通知中添加了一个按钮

but I don't know how to have it call a function when it's clicked.

但我不知道如何让它在点击时调用一个函数。

I tried an approach like this https://code.google.com/p/languagepickerwidget/source/browse/trunk/trunk/src/org/gnvo/langpicker/LangPicker.javasince it's also using a RemoteViews object but nothing happens when I click the button.

我尝试了这样的方法https://code.google.com/p/languagepickerwidget/source/browse/trunk/trunk/src/org/gnvo/langpicker/LangPicker.java因为它也使用 RemoteViews 对象但什么时候都没有发生我点击按钮。

This is what I currently have:

这是我目前拥有的:

private void createNotification(){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

    Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());
    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_switch);

    //the intent that is started when the notification is clicked (works)
    Intent notificationIntent = new Intent(this, SettingsActivity.class);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.contentView = notificationView;
    notification.contentIntent = pendingNotificationIntent;
    notification.flags |= Notification.FLAG_NO_CLEAR;

    //this is the intent that is supposed to be called when the button is clicked
    Intent switchIntent = new Intent(this, switchButtonListener.class);
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);

    notificationView.setOnClickPendingIntent(R.id.buttonswitch, pendingSwitchIntent);

    notificationManager.notify(1, notification);
}

public static class switchButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TAG", "test");
    }

}

I can start an activity with the button but I didn't succeed to have it call a simple function. What would be the best way to do this?

我可以用按钮开始一个活动,但我没有成功让它调用一个简单的函数。什么是最好的方法来做到这一点?

Edit:I found out that I had to register "switchButtonListener" in AndroidManifest.xml

编辑:我发现我必须在 AndroidManifest.xml 中注册“switchButtonListener”

<receiver android:name="SettingsActivity$switchButtonListener" />

Source: Android Activity with no GUI

来源:没有 GUI 的 Android Activity

It works now.

它现在有效。

回答by A E

I found out that I had to register "switchButtonListener" in AndroidManifest.xml

我发现我必须在 AndroidManifest.xml 中注册“switchButtonListener”

<receiver android:name="SettingsActivity$switchButtonListener" />

Source: Android Activity with no GUI

来源:没有 GUI 的 Android Activity



Later I found out that I can also use code like this to achieve the same thing without modifying the manifest.

后来我发现我也可以使用这样的代码来实现同样的事情,而无需修改清单。

switchButtonListener = new SwitchButtonListener();
registerReceiver(switchButtonListener, new IntentFilter(SWITCH_EVENT));

.

.

public class switchButtonListener extends BroadcastReceiver {
@Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TAG", "test");
    }

}

.

.

Intent switchIntent = new Intent(LangService.SWITCH_EVENT);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context, 0, switchIntent, 0);

notificationView.setOnClickPendingIntent(R.id.buttonswitch, pendingSwitchIntent);


Note that this way I can declare the switchButtonListener class without the static attribute (if not static, it would crash in the previous example) giving me much more flexibility.
Don't forget to call unregisterReceiver() later.


请注意,通过这种方式我可以声明没有静态属性的 switchButtonListener 类(如果不是静态的,它会在前面的示例中崩溃)给我更多的灵活性。
不要忘记稍后调用 unregisterReceiver()。