我试图制作一个可以显示所有wifi接入点列表的wifi扫描器。我的代码如下:
MainActivity.java:
package com.example.wifiscanner;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
WifiManager wifi;
WifiScanReceiver wifireciever;
Button scan;
List<ScanResult> wifilist;
ListView list;
String wifis[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
list=(ListView)findViewById(R.id.listView1);
scan=(Button)findViewById(R.id.button1);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()==false){
wifi.setWifiEnabled(true);
}
wifireciever = new WifiScanReceiver();
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();
}
});
}
protected void onPause() {
unregisterReceiver(wifireciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifilist = wifi.getScanResults();
wifis = new String[wifilist.size()];
for(int i = 0; i < wifilist.size(); i++){
wifis[i] = ((wifilist.get(i)).toString());
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,wifis));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.wifiscanner.MainActivity$PlaceholderFragment" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true" >
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/listView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="37dp"
android:minWidth="@dimen/abc_action_bar_stacked_tab_max_width"
android:text="Scan" />
</RelativeLayout>
Log cat:
05-26 09:41:49.840: D/AndroidRuntime(11186): Shutting down VM
05-26 09:41:49.840: W/dalvikvm(11186): threadid=1: thread exiting with uncaught exception (group=0x414f82a0)
05-26 09:41:49.861: E/AndroidRuntime(11186): FATAL EXCEPTION: main
05-26 09:41:49.861: E/AndroidRuntime(11186): java.lang.SecurityException: WifiService: Neither user 10105 nor current process has android.permission.ACCESS_WIFI_STATE.
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Parcel.readException(Parcel.java:1425)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Parcel.readException(Parcel.java:1379)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.net.wifi.IWifiManager$Stub$Proxy.getWifiEnabledState(IWifiManager.java:900)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.net.wifi.WifiManager.getWifiState(WifiManager.java:1078)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.net.wifi.WifiManager.isWifiEnabled(WifiManager.java:1090)
05-26 09:41:49.861: E/AndroidRuntime(11186): at com.example.wifiscanner.MainActivity$1.onClick(MainActivity.java:44)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.view.View.performClick(View.java:4262)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.view.View$PerformClick.run(View.java:17421)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Handler.handleCallback(Handler.java:615)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Looper.loop(Looper.java:137)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.app.ActivityThread.main(ActivityThread.java:4947)
05-26 09:41:49.861: E/AndroidRuntime(11186): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 09:41:49.861: E/AndroidRuntime(11186): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 09:41:49.861: E/AndroidRuntime(11186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-26 09:41:49.861: E/AndroidRuntime(11186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-26 09:41:49.861: E/AndroidRuntime(11186): at dalvik.system.NativeStart.main(Native Method)
05-26 09:41:49.941: D/dalvikvm(11186): GC_CONCURRENT freed 203K, 15% free 6966K/8135K, paused 12ms+9ms, total 73ms
05-26 09:42:00.201: I/Process(11186): Sending signal. PID: 11186 SIG: 9
我的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wifiscanner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.wifiscanner.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
一旦我按下扫描按钮,它就会停止工作。如果有人能帮助我,我会很感激。
答案 0 :(得分:2)
你logcat清楚地说过
java.lang.SecurityException: WifiService: Neither user 10105 nor current process has android.permission.ACCESS_WIFI_STATE.
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Parcel.readException(Parcel.java:1425)
在android.permission.ACCESS_WIFI_STATE
manifest.xml
答案 1 :(得分:0)
您需要将以下权限添加到清单文件中:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这些需要在应用程序标记之外添加。 (在之前或之后)
您的清单文件位于项目的主文件夹中,名称为:AndroidManifest.xml。