我想获得一些关于我所连接的 wifi 的信息,但我不断收到关于我的权限的错误,但我想他们应该没问题。你能帮帮我吗?
我的第一行logcat
描述了这个问题。这条线是我打电话的地方wifi.getConnectionInfo();
我的java代码:
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// private ConnectivityReceiver receiver = null;
// private TextView txtNetworkInfo = null;
private WifiManager wifi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
b.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
wifi.getConnectionInfo();
// startService(new Intent(MainActivity.this, CheckService.class));
}
});
b2.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
// stopService(new Intent(MainActivity.this, CheckService.class));
}
});
}
}
还有我的日志猫-
11-15 18:08:37.583: E/AndroidRuntime(28200): FATAL EXCEPTION: main
11-15 18:08:37.583: E/AndroidRuntime(28200): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jesper.soundchange/com.jesper.soundchange.MainActivity}: java.lang.SecurityException: WifiService: Neither user 10067 nor current process has android.permission.ACCESS_WIFI_STATE.
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.os.Looper.loop(Looper.java:144)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.app.ActivityThread.main(ActivityThread.java:4937)
11-15 18:08:37.583: E/AndroidRuntime(28200): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 18:08:37.583: E/AndroidRuntime(28200): at java.lang.reflect.Method.invoke(Method.java:521)
11-15 18:08:37.583: E/AndroidRuntime(28200): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-15 18:08:37.583: E/AndroidRuntime(28200): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-15 18:08:37.583: E/AndroidRuntime(28200): at dalvik.system.NativeStart.main(Native Method)
11-15 18:08:37.583: E/AndroidRuntime(28200): Caused by: java.lang.SecurityException: WifiService: Neither user 10067 nor current process has android.permission.ACCESS_WIFI_STATE.
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.os.Parcel.readException(Parcel.java:1247)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.os.Parcel.readException(Parcel.java:1235)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.net.wifi.IWifiManager$Stub$Proxy.getDhcpInfo(IWifiManager.java:754)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.net.wifi.WifiManager.getDhcpInfo(WifiManager.java:712)
11-15 18:08:37.583: E/AndroidRuntime(28200): at com.jesper.soundchange.MainActivity.onCreate(MainActivity.java:30)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
11-15 18:08:37.583: E/AndroidRuntime(28200): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
我的清单——
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jesper.soundchange"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<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" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".CheckService"
android:label="My Service" >
</service>
</application>