gpt4 book ai didi

Android 使用广播检查互联网连接

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:03 26 4
gpt4 key购买 nike

我想为我的互联网连接检查实现一个广播接收器。如果连接不存在就完成();它。但我仍然搞砸了上下文。请检查我的以下代码。

/**
* This broadcast receiver is awoken after boot and registers the service that
* checks for new photos from all the known contacts.
*/


public class ConnectionDetector extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {



boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false);

if(noConnectivity){

((Activity)context).finish();
//Show Warning Message
//Close Application the way i suggested
}

}


}

安卓 list

 <receiver      android:name=".ConnectionDetector"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>

登录目录:

11-01 22:40:29.179: E/AndroidRuntime(29531): FATAL EXCEPTION: main
11-01 22:40:29.179: E/AndroidRuntime(29531): java.lang.RuntimeException: Unable to start receiver in.wptrafficanalyzer.actionbarsherlocknavtab.ConnectionDetector: java.lang.ClassCastException: android.app.ReceiverRestrictedContext
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.os.Looper.loop(Looper.java:130)
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread.main(ActivityThread.java:3691)
11-01 22:40:29.179: E/AndroidRuntime(29531): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 22:40:29.179: E/AndroidRuntime(29531): at java.lang.reflect.Method.invoke(Method.java:507)
11-01 22:40:29.179: E/AndroidRuntime(29531): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-01 22:40:29.179: E/AndroidRuntime(29531): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-01 22:40:29.179: E/AndroidRuntime(29531): at dalvik.system.NativeStart.main(Native Method)
11-01 22:40:29.179: E/AndroidRuntime(29531): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext
11-01 22:40:29.179: E/AndroidRuntime(29531): at in.wptrafficanalyzer.actionbarsherlocknavtab.ConnectionDetector.onReceive(ConnectionDetector.java:29)
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
11-01 22:40:29.179: E/AndroidRuntime(29531): ... 10 more

最佳答案

您在 BroadcastReciever 中获得的上下文不是 Activity。 BroadcastReciever 在 Activity 之外工作并且不依赖于 Activity,除非您专门制作它。

抛开在不通知用户的情况下完成互联网 Activity 的不良做法,您可以执行以下操作:

public abstract class ConnectionAwareActivity extends Activity {

protected final IntentFilter mIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); // A filter for a BR. We want to listen to internet changes
protected final ConnectionDetector mConnectionDetector = new ConnectionDetector(); // Creating an instance of our BR for activity to use

@Override
protected void onResume() {
super.onResume();
try {
registerReceiver(mConnectionDetector, mIntentFilter); // Activity gets shown, we register a BR and it starts to receive notifications about internet changes
} catch (Exception exc) {
// whoops
}
}

@Override
protected void onPause() {
super.onPause();
try {
unregisterReceiver(mConnectionDetector); // Try to unregister BR, since when activity is not visible to user, we don't want to perform any operations on internet change
} catch (Exception exc) {
// whoops
}
}

// Your BR that is encapsulated in Activity and therefore has access to it's methods, since it has access to Activity instance
protected class ConnectionDetector extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (noConnectivity) {
finish();
}

}
}

将它用作其他 Activity 的父类(super class)(这个 Activity 从 Activity 扩展,将其更改为任何内容),必须在连接错误时终止。

这是非常基本的变体并且有些错误,因为您更喜欢组合而不是父类(super class)

关于Android 使用广播检查互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13179487/

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