从一个BroadcastReceiver访问ř [英] Accessing R from a BroadcastReceiver

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

问题描述

我想换一个TextView在我的BroadcastReceiver的的onReceive方法,但与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.

我如何才能访问?

推荐答案

定义一个接口,并使用回调,让活动知道广播事件已收到。

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
}

请注意。由于您使用的接口,任何类(不只是一个活动),可以实现它,所以你可以在你的应用程序的任何地方进行更新。该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,或任何与用户界面,因为你的活动是知道的唯一一类,并可以改变,你的用户界面 - !这是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.

想想接口作为合同的。它说,任何人谁实现它,必须实施的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和字符串作为参数

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
   }
}

这篇关于从一个BroadcastReceiver访问ř的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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