Android用户权限错误

时间:2014-10-09 13:57:04

标签: java android

我有一个错误:

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

我尝试获取wifi名称(ssid)。我使用这段代码:

WifiManager wifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE);
System.out.println(wifiManager.getConnectionInfo ().getSSID ());

在我的清单文件中,我添加了此权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

我做错了什么? Thisthis对我不起作用 谢谢

我的完整logcat:

10-09 16:08:23.705  31988-31988/com.example.android.navigationdrawerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.MainActivity}: java.lang.SecurityException: WifiService: Neither user 10132 nor current process has android.permission.ACCESS_WIFI_STATE.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
            at android.app.ActivityThread.access$700(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5319)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.SecurityException: WifiService: Neither user 10132 nor current process has android.permission.ACCESS_WIFI_STATE.
            at android.os.Parcel.readException(Parcel.java:1425)
            at android.os.Parcel.readException(Parcel.java:1379)
            at android.net.wifi.IWifiManager$Stub$Proxy.getConnectionInfo(IWifiManager.java:991)
            at android.net.wifi.WifiManager.getConnectionInfo(WifiManager.java:976)
            at com.example.android.navigationdrawerexample.MainActivity$PlanetFragment.onCreateView(MainActivity.java:261)
            at android.app.Fragment.performCreateView(Fragment.java:1699)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
            at android.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
            at android.app.Activity.performStart(Activity.java:5335)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
            at android.app.ActivityThread.access$700(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5319)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:2)

考虑到您使用了正确的权限,并且这些行在语法上是正确的,您可能错误地放置了清单中的权限,如果您将它们放在其他地方但根目录下清单xml节点,它们可能根本不起作用。他们需要 <{strong> <manifest> 之后 </manifest>,而不是<application><activity>

<manifest xlmns:android...>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 ...YOUR APPLICATION & ACTIVITY HERE...
</manifest>

请注意,清单标签已经过简化以便于阅读

<uses-permission>

答案 1 :(得分:-1)

将此添加到AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

对我来说没关系!

答案 2 :(得分:-1)

User

答案 3 :(得分:-2)

当你改变wifi状态时,这也需要这个。

试试这个。

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

在AndroidManifest.xml中添加此权限列表:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />