搬运机器人的通知里面的按钮 [英] Handling buttons inside android notifications

查看:165
本文介绍了搬运机器人的通知里面的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个按钮,通知里面

I added a button inside a notification

但我不知道如何使用它调用一个函数时,它的点击。

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

我想这样的方法<一href="https://$c$c.google.com/p/languagepickerwidget/source/browse/trunk/trunk/src/org/gnvo/langpicker/LangPicker.java">https://$c$c.google.com/p/languagepickerwidget/source/browse/trunk/trunk/src/org/gnvo/langpicker/LangPicker.java因为它也使用RemoteViews对象,但没有任何反应,当我点击该按钮。

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

这是我目前有:

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?

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

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

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

来源: Android的活动,没有GUI

它的工作原理吧。

推荐答案

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

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

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

来源: Android的活动,没有GUI

后来我才发现原来我也可以用code这样来达到同样的事情,而无需修改清单。

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);


需要注意的是这种方式,我可以不用静态属性声明switchButtonListener类(如果不是一成不变的,它会崩溃在previous例子)给了我更大的灵活性。
不要忘了以后调用unregisterReceiver()。


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.

这篇关于搬运机器人的通知里面的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆