从广播接收器访问 R [英] Accessing R from a BroadcastReceiver

查看:18
本文介绍了从广播接收器访问 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 BroadcastReceiver 的 onReceive 方法中更改一个 TextView,但我无法使用 findViewById 访问它,因为它不是一个活动.我不想在我的活动中为 BroadcastReceiver 创建一个私有类.

I want to change a TextView in the onReceive method of my BroadcastReceiver, but I can't access it with findViewById because it's not an activity. I don't want to create a private class for the BroadcastReceiver in my activity.

我如何获得访问权限?

推荐答案

定义一个接口并使用回调让activity知道收到了一个广播事件.

Define an interface and use a callback to let the activity know that a broadcast event has been received.

public Interface BroadcastReceiverListener {
    void onReceive(int arg1, String arg2); ..<----add arguments you want to pass back
}

在您的 BroadcastReceiver 类中

In your BroadcastReceiver class

ArrayList<BroadcastReceiveListener > listeners = new ArrayList<BroadcastReceiveListener >();

...

public void addBroadcastReceiveListener (BroadcastReceiveListener listener){
    if(!listeners.contains(listener)){
        listeners.add(listener);
    }
}

public void removeBroadcastReceiveListener (BroadcastReceiveListener listener){
    if(listeners.contains(listener)){
        listeners.remove(listener);
    }
}

在您的 OnReceive 中

In your OnReceive

for (BroadcastReceiveListener listener:listeners){
   listener.onReceive(arg1, arg2);
}

在您的活动中:

public class MyActivity extends Activity implements BroadcastReceiveListener {

    ...

    broadcastReceiver.addBroadcastReceiveListener(this); <---- the instance of your receiver

    ...
}

public void onReceive(int arg1, String arg2){
   // do whatever you need to do
}

注意.因为您使用接口,所以任何类(不仅仅是 Activity)都可以实现它,因此您可以在应用程序的任何位置进行更新.BroadcastReceiver 类不知道也不关心.它只是调用监听器,如果有的话.

Note. Because you use an interface, any class (not just an Activity) can implement it so you can update anywhere in your app. The BroadcastReceiver class doesn't know or care. It just calls the listeners, if any are registered.

现在,您不需要访问 R 或与 UI 相关的任何内容,因为您的 Activity 是唯一知道并且可以更改您的 UI 的类 - 这是 Android 的方式!

Now, you don't need to access R, or anything to do with the UI since your Activity is the only class that knows about, and can change, your UI - which is the Android way!

参数是你需要的任何东西.

The arguments are whatever you need them to be.

Interface 视为合约.它说任何实现它的人都必须实现 onReceive() 方法,并且该方法将使用整数和字符串调用.如果有的话,这取决于你需要什么论据.

Think of the Interface as a contract. It says that anyone who implements it, must implement the onReceive() method and that the method will be called with an integer and a String. It's up to you what arguments you need, if any.

BroadcastReceiver.onReceive()调用接口的onReceive回调,传入int和String作为参数.

BroadcastReceiver.onReceive() calls the onReceive callback of the interface and passes in the int and String as arguments.

例如,您可以更改接口定义以传递布尔值.

You could change the Interface definition to pass a bool for example.

public Interface BroadcastReceiverListener {
    void onReceive(boolean arg1); ..<----add arguments you want to pass back
}

那么你的调用者看起来像这样:

Then your caller looks like this:

for (BroadcastReceiveListener listener:listeners){
   listener.onReceive(someBooleanValue);
}

你的回调看起来像这样:

And your callback looks like this:

public void onReceive(boolean theCallerIsReady){
   if(theCallerIsReady){
       // do interesting stuff
   }
}

这篇关于从广播接收器访问 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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