gpt4 book ai didi

android - Android 中的互联网连接检查

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

我需要在 Android 中检查互联网是否可用,所以我尝试使用以下功能:

boolean b_IsConnect = isNetworkAvailable();
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}

而且我还在 list 中添加了所需的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

但它仍然抛出异常:

05-02 16:05:26.226: E/AndroidRuntime(481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.housedisplay/com.housedisplay.RssActivity}: java.lang.SecurityException: ConnectivityService: Neither user 10038 nor current process has android.permission.ACCESS_NETWORK_STATE.
05-02 16:05:26.226: E/AndroidRuntime(481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.os.Looper.loop(Looper.java:126)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.app.ActivityThread.main(ActivityThread.java:3997)
05-02 16:05:26.226: E/AndroidRuntime(481): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:05:26.226: E/AndroidRuntime(481): at java.lang.reflect.Method.invoke(Method.java:491)
05-02 16:05:26.226: E/AndroidRuntime(481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-02 16:05:26.226: E/AndroidRuntime(481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-02 16:05:26.226: E/AndroidRuntime(481): at dalvik.system.NativeStart.main(Native Method)
05-02 16:05:26.226: E/AndroidRuntime(481): Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10038 nor current process has android.permission.ACCESS_NETWORK_STATE.
05-02 16:05:26.226: E/AndroidRuntime(481): at android.os.Parcel.readException(Parcel.java:1322)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.os.Parcel.readException(Parcel.java:1276)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:446)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:256)
05-02 16:05:26.226: E/AndroidRuntime(481): at com.housedisplay.RssActivity.isNetworkAvailable(RssActivity.java:218)
05-02 16:05:26.226: E/AndroidRuntime(481): at com.housedisplay.RssActivity.onCreate(RssActivity.java:59)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-02 16:05:26.226: E/AndroidRuntime(481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)

你能告诉我这里发生了什么吗?

最佳答案

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >缺少结束斜杠。可能导致解析 XML 时出现问题,这可能导致下一个 <uses-permission ...>无法正确解析。

关于android - Android 中的互联网连接检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10412099/

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