gpt4 book ai didi

带有监听器的 Android Broadcastreceiver

转载 作者:行者123 更新时间:2023-11-29 17:43:03 25 4
gpt4 key购买 nike

我有一个用于监控网络状态的 Broadcastreceiver。当它发生变化时,我想向我的 BaseActivity 发送自定义异常,以便可以在任何 Activity 中显示。

为了发送异常,我有一个监听器,它是在我的 BaseActivity 上实现的。

广播接收者:

public class ConnectionStateReceiver extends BroadcastReceiver {
private ConnectionStateListener connectionStateListener;

public void onReceive(Context context, Intent intent) {
this.connectionStateListener = (ConnectionStateListener) context;

if (this.connectionStateListener != null) {
this.connectionStateListener.showConnectionStateLostException(new NetworkConnectionException());
}
......

听众:

public interface ConnectionStateListener {
void showConnectionStateLostException(Exception exception);
}

基础 Activity :

public abstract class BaseActivity extends ActionBarActivity implements ConnectionStateListener, BatteryChangeListener {

...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
this.batteryStatus = this.registerReceiver(null, ifilter);
//Receiver called when battery level changes
this.systemBatteryLevelReceiver = new BatteryChangeReceiver();

registerReceiver(systemBatteryLevelReceiver, ifilter);

}

...
@Override
public void showConnectionStateLostException(Exception exception) {
showSystemStateMessage(ExceptionMessageUtils.createSystemExceptionMessage(this, exception));
}

@Override
public void showChangeBatteryException(Exception exception) {
showSystemStateMessage(ExceptionMessageUtils.createSystemExceptionMessage(this, exception));
}
...

list :

...
<receiver android:name=".data.broadcastreceivers.ConnectionStateReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>

问题是当它试图在接收器上进行转换时:

this.connectionStateListener = (ConnectionStateListener) context;

它给我以下错误:

01-14 11:45:38.347    8766-8766/com.massfactory.mobile.android.stlparatransit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.massfactory.mobile.android.stlparatransit, PID: 8766
java.lang.RuntimeException: Unable to start receiver com.massfactory.mobile.android.stlparatransit.data.broadcastreceivers.ConnectionStateReceiver: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to com.massfactory.mobile.android.stlparatransit.domain.broadcastlisteners.ConnectionStateListener at android.app.ActivityThread.handleReceiver(ActivityThread.java:2593) at android.app.ActivityThread.access$1700(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5274)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to com.massfactory.mobile.android.stlparatransit.domain.broadcastlisteners.ConnectionStateListener
at com.massfactory.mobile.android.stlparatransit.data.broadcastreceivers.ConnectionStateReceiver.onReceive(ConnectionStateReceiver.java:37)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
        at android.app.ActivityThread.access$1700(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5274)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我有另一个广播,它使用相同的“结构”监视电池状态并且没有给出任何问题,唯一的区别是“电池广播”是动态声明的(不像这个在 list 中)。

电池监听器:

public interface BatteryChangeListener {
void showChangeBatteryException(Exception exception);
}

知道为什么会发生这种情况以及我应该如何解决它吗?

提前致谢

最佳答案

解决方案是将 ConnectionStateReceiver 注册到 Activity 中,并按照 EagleEye 的评论将其从 list 中删除。

public abstract class BaseActivity extends ActionBarActivity implements ConnectionStateListener, BatteryChangeListener {

...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
this.batteryStatus = this.registerReceiver(null, ifilter);
//Receiver called when battery level changes
this.systemBatteryLevelReceiver = new BatteryChangeReceiver();

registerReceiver(systemBatteryLevelReceiver, ifilter);


//Check Network State
this.iFilterConnectionState = new IntentFilter(); this.iFilterConnectionState.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
this.systemConnectionStateReceiver = new ConnectionStateReceiver();
registerReceiver(this.systemConnectionStateReceiver, iFilterConnectionState);

}

关于带有监听器的 Android Broadcastreceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27941328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com