读取Wifi/3G状态异常

发布于 2024-11-07 14:33:14 字数 1695 浏览 0 评论 0原文

您好,我有以下读取网络/Wifi 状态的代码:

public String getMacAddress() {
    WifiManager wm = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    return wm.getConnectionInfo().getMacAddress();
}

这会抛出

Caused by: java.lang.SecurityException: WifiService: Neither user 10032 nor current process has android.permission.ACCESS_WIFI_STATE

public boolean is3G() {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    return !(connectivityManager.getNetworkInfo(
            ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || connectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED);
}

会抛出

Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10032 nor current process has android.permission.ACCESS_NETWORK_STATE.

在有人告诉我有关权限的信息之前,这是我的清单(相关部分):

<manifest> 
   <!-- usual tags for activitys... etc.. -->

    <uses-permission android:name="android.permission.WRITE_OWNER_DATA" />
    <uses-permission android:name="android.permission.READ_OWNER_DATA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <user-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest> 

Hi I have the following code that reads Network/Wifi state:

public String getMacAddress() {
    WifiManager wm = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    return wm.getConnectionInfo().getMacAddress();
}

This throws

Caused by: java.lang.SecurityException: WifiService: Neither user 10032 nor current process has android.permission.ACCESS_WIFI_STATE

And

public boolean is3G() {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    return !(connectivityManager.getNetworkInfo(
            ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || connectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED);
}

This throws

Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10032 nor current process has android.permission.ACCESS_NETWORK_STATE.

Before someone tell me about the permissions, that is my Manifest (the relevant part):

<manifest> 
   <!-- usual tags for activitys... etc.. -->

    <uses-permission android:name="android.permission.WRITE_OWNER_DATA" />
    <uses-permission android:name="android.permission.READ_OWNER_DATA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <user-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2024-11-14 14:33:14

您拼错了最新 2 个权限:

useR-permission 而不是 useS-permission

You've mispelled the latest 2 permissions:

useR-permission instead of useS-permission

去了角落 2024-11-14 14:33:14

您的清单权限错误。

您拥有用户权限而不是使用权限

Your manifest permissions are wrong.

you have user-permission instead of uses-permission

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文