不准叫我自己的方法(QUOT;静态"谜)自己的、叫我、静态、方法

2023-09-07 11:03:57 作者:╮( ̄▽ ̄)╭

我有一个BroadcastReceiver作为一个单独的类文件,听着从后台服务传感器操作。我想让它显示的场合屏幕上的文字。

我要BroadcastClass来电显示在我的Activity类的方法,它需要照顾的字符串。但我的广播接收器类不能调用它们不是静态的方法(?)的不能让一个静态引用非静态方法showString(字符串)从类型myActivity的

如果我做静态的特定方法在我的活动类,它有效地没有找到findViewById再失去它自己活动的UI接触的不能让一个静态引用非静态方法findViewById(INT )从类型的活动。的

在另一个小应用程序,我有一个BroadcastReceiver作为一个Activity类文件中的内部类。然后,我没有问题,从它直接访问UI。然后我就声明为类文件领域的相关意见。但我想构建更好的东西,分离广播接收和分析用户界面。它变得如此凌乱有一个整个应用程序源$ C ​​$ c和相同的文件。

这是什么意思,以创建活动的实例?我应该做的,在活动本身还是在广播接收器?我不希望我的UI的多个实例,但我可能需要从几个不同的类访问它。

编辑加法:我试图从一本书此singleton模式,但myActivity不能被实例化,获得构造不允许的。进入前的onCreate崩溃:

 公共类MyStartupActivity延伸活动{    私有静态MyStartupActivity uniqueInstance;    私人MyStartupActivity(){}    公共静态MyStartupActivity实例化(){        如果(uniqueInstance == NULL){            uniqueInstance =新MyStartupActivity();        }        返回uniqueInstance;    } 

解决方案 美联英语盘点最流行实用的英文 客套话

您将不得不增加一个字段为活动对象在你的广播接收器类,如下:

 类MyBroadcastReceiver扩展广播接收器{    私人最终活动活动;    公共MyBroadcastReceiver(活动活动){        this.activity =活动;    }    公共无效的onReceive(...){        视图V = activity.findViewById(ID);        // ...    }} 

初​​始化像这样

 类MyActivity延伸活动{    公共无效的onCreate(...){        MyBroadcastReceiver接收器=新MyBroadcastReceiver(本);        // ...    }} 

编辑:如果你不想每次都instatiate接收机活动创建,您可以(例如)介绍setter才能 MyBroadcastReceiver

 类MyBroadcastReceiver扩展广播接收器{    私人活动活动;    公共无效setActivity(活动活动){        this.activity =活动;    }    公共无效的onReceive(...){        如果(活动!= NULL){            视图V = activity.findViewById(ID);            // ...        }其他{            //我们没有一个活动,做任何你也离不开它......        }        // ...    }} 

和在活动中使用它:

 类MyActivity延伸活动{    私有静态MyBroadcastReceiver接收器;    公共无效的onCreate(...){        如果(接收== NULL){            接收器=新MyBroadcastReceiver();            // ...        }        receiver.setActivity(本);        // ...    }} 

I have a BroadcastReceiver as a separate class file, listening to sensor actions from a service in the background. I want it to show text on the screen on occassions.

I want the BroadcastClass to call a method in my Activity class which takes care of the string to be shown. But my BroadcastReceiver class cannot call methods which are not static(?) "Cannot make a static reference to the non-static method showString(String) from the type myActivity"

And if I make the particular method static in my Activity class, it effectively looses contact with the UI of its own activity by not finding findViewById anymore "Cannot make a static reference to the non-static method findViewById(int) from the type Activity".

In another small app, I had a BroadcastReceiver as an internal class inside an Activity class file. Then I had no problems accessing the UI from it directly. I then had the relevant Views declared as class file fields. But I'd like to structure things better, separating UI from broadcast receiving and analysis. It gets so messy to have the entire application source code in one and the same file.

What does it mean to create instances of an Activity? Should I do that in the Activity itself or in the BroadcastReceiver? I don't want more than one instance of my UI, but I might need to access it from several different classes.

EDIT ADDITION: I try this singleton pattern from a book, but myActivity cannot be instantiated, access to constructor not allowed. Crash before onCreate is entered:

public class MyStartupActivity extends Activity {
    private static MyStartupActivity uniqueInstance;
    private MyStartupActivity() {}
    public static MyStartupActivity instantiate() {
        if (uniqueInstance == null) {
            uniqueInstance = new MyStartupActivity();
        }
        return uniqueInstance;
    }

解决方案

You'll have to add a field for the Activity object in your BroadcastReceiver class, like this:

class MyBroadcastReceiver extends BroadcastReceiver {
    private final Activity activity;

    public MyBroadcastReceiver(Activity activity) {
        this.activity = activity;
    }

    public void onReceive(...) {
        View v = activity.findViewById(ID);
        // ...
    }
}

Initialize it like this

class MyActivity extends Activity {
    public void onCreate(...) {
        MyBroadcastReceiver receiver = new MyBroadcastReceiver(this);
        // ...
    }
}

EDIT: If you don't want to instatiate the receiver every time the Activity is created, you can (for example) introduce a setter to MyBroadcastReceiver:

class MyBroadcastReceiver extends BroadcastReceiver {
    private Activity activity;

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void onReceive(...) {
        if (activity != null) {
            View v = activity.findViewById(ID);
            // ...
        } else {
            // we don't have an activity, do whatever you can do without it...
        }
        // ...
    }
}

and use it in the activity:

class MyActivity extends Activity {
    private static MyBroadcastReceiver receiver;

    public void onCreate(...) {
        if (receiver == null) {
            receiver = new MyBroadcastReceiver();
            // ...
        }
        receiver.setActivity(this);

        // ...
    }
}