15

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

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

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

这是我目前拥有的:

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

}

我可以使用按钮启动一个活动,但我没有成功让它调用一个简单的函数。最好的方法是什么?

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

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

来源:没有 GUI 的 Android Activity

现在可以了。

4

2 回答 2

32

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

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

来源:没有 GUI 的 Android Activity


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

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 类(如果不是静态的,它会在前面的示例中崩溃)给我更多的灵活性。
不要忘记稍后调用 unregisterReceiver() 。

于 2012-09-16T21:51:00.830 回答
0

在 Kotlin 中,您可以使用匿名类注册接收器。

const val STOP_ALARM_ACTION = "STOP_ALARM"

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // ...
        registerReceiver(object : BroadcastReceiver() {
            override fun onReceive(p0: Context?, p1: Intent?) {
                stopAlarm();
            }
        }, IntentFilter(STOP_ALARM_ACTION))
    }
    private fun playAlarm() {
        ringtone.stop()
        val stopIntent = Intent(STOP_ALARM_ACTION)
        val stopPendingIntent = PendingIntent.getBroadcast(this, 0, stopIntent, 0)
        val notification = NotificationCompat.Builder(this, "Timer1_ALARM")
                // ...
                .addAction(android.R.drawable.ic_delete, "Stop", stopPendingIntent)
                .build()
        // ...
    }
    private fun stopAlarm() {
        ringtone.stop()
    }
}
于 2022-02-26T13:08:44.673 回答