我正在嘗試爲我的項目製作無線掃描儀,當我點擊掃描按鈕時掃描20次。當我運行代碼掃描,但我不知道它掃描20次或沒有,RSSI值/ wifi級別不斷變化。在日誌中還說,你需要取消註冊回收。掃描20次的Wifi掃描儀
我MainActivity.java
package com.example.scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
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.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
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;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
WifiManager wifi;
WifiScanReceiver wifireciever;
WifiInfo info;
Button scan, save;
List<ScanResult> wifilist;
ListView list;
String wifis[];
String name;
@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);
save=(Button)findViewById(R.id.button2);
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));
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savedata();
}
});
}
protected void savedata() {
// TODO Auto-generated method stub
try {
File sdcard = Environment.getExternalStorageDirectory();
File directory = new File(sdcard.getAbsolutePath() + "/WIFI_RESULT");
directory.mkdirs();
name = new SimpleDateFormat("yyyy-MM-dd HH mm ss").format(new Date());
File file = new File(directory,name + "wifi_data.txt");
FileOutputStream fou = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fou);
try {
for (int i =0; i < list.getCount(); i++){
osw.append(list.getItemAtPosition(i).toString());
}
osw.flush();
osw.close();
Toast.makeText(getBaseContext(), "Saved", Toast.LENGTH_LONG).show();
} catch (IOException e){
e.printStackTrace();
}
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
public class scan_data {
String ssid;
String bssid;
int lvl;
int count = 0;
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
scan_data[] data = new scan_data[100];
for (int i=0;i<100;i++){
scan_data sdata = new scan_data();
data[i]=sdata;
}
int a =0;
for (int i=0;i<20;i++){
wifi.startScan();
List<ScanResult> wifilist = wifi.getScanResults();
if (a<wifilist.size()){
a=wifilist.size();
}
if (i==0){
for(int j=0;j<wifilist.size();j++){
data[j].ssid=wifilist.get(j).SSID.toString();
data[j].bssid=wifilist.get(j).BSSID.toString();
data[j].lvl=wifilist.get(j).level;
data[j].count++;
}
}
else if (i==19){
for (int j=0;j<wifilist.size();j++){
if(data[j].bssid.equals(wifilist.get(j).BSSID)){
data[j].lvl=data[j].lvl + wifilist.get(j).level;
data[j].count++;
}
data[j].lvl=data[j].lvl/data[j].count;
}
}
else {
for (int j=0;j<wifilist.size();j++){
if(data[j].bssid.equals(wifilist.get(j).BSSID)){
data[j].lvl=data[j].lvl + wifilist.get(j).level;
data[j].count++;
}
}
}
}
wifis = new String[a];
for (int i =0; i<a; i++){
wifis[i] = ("\n" + data[i].ssid + "\n AP Address" + data[i].bssid + "\n Signal Strength:" + data[i].lvl).toString();
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,wifis));
}
}
protected void onPause() {
unregisterReceiver(wifireciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
@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;
}
}
和日誌貓:
06-05 17:42:43.262: D/dalvikvm(32156): GC_FOR_ALLOC freed 117K, 11% free 6899K/7687K, paused 35ms, total 35ms
06-05 17:42:43.312: I/dalvikvm-heap(32156): Grow heap (frag case) to 13.478MB for 6400016-byte allocation
06-05 17:42:43.412: D/dalvikvm(32156): GC_CONCURRENT freed 1K, 6% free 13148K/13959K, paused 33ms+2ms, total 103ms
06-05 17:42:43.412: D/dalvikvm(32156): WAIT_FOR_CONCURRENT_GC blocked 8ms
06-05 17:42:43.532: D/AbsListView(32156): Get MotionRecognitionManager
06-05 17:42:43.642: D/libEGL(32156): loaded /system/lib/egl/libGLES_rhea.so
06-05 17:42:43.662: D/BRCM_EGL(32156): eglCreateContext() config: 19 context: 0x4a12ca38, VC context 1, Thread 32156
06-05 17:42:43.662: D/BRCM_EGL(32156): eglCreateWindowSurface() surface: 0x4a240960, VC surface: 1, Thread: 32156
06-05 17:42:43.662: D/BRCM_EGL(32156): eglMakeCurrent(0x4a12ca38, 0x4a240960, 0x4a240960) Thread: 32156
06-05 17:42:43.672: D/OpenGLRenderer(32156): Enabling debug mode 0
06-05 17:43:39.937: D/BRCM_EGL(32156): eglMakeCurrent(NULL) Thread: 32156
06-05 17:43:39.937: D/BRCM_EGL(32156): eglDestroySurface() surface: 0x4a240960, android window 0x4908d370, Thread: 32156
06-05 17:43:40.457: D/BRCM_EGL(32156): eglMakeCurrent(0x4a12ca38, 0x4a2cbf60, 0x4a2cbf60) Thread: 32156
06-05 17:43:40.478: D/BRCM_EGL(32156): eglMakeCurrent(NULL) Thread: 32156
06-05 17:43:40.488: D/BRCM_EGL(32156): eglMakeCurrent(0x4a12ca38, 0x4a2cbf60, 0x4a2cbf60) Thread: 32156
06-05 17:43:40.488: D/BRCM_EGL(32156): eglMakeCurrent(NULL) Thread: 32156
06-05 17:43:40.508: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.508: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.508: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.508: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.508: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.508: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.508: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.508: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.508: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.508: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.508: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.508: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.508: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.508: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
06-05 17:43:40.518: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.518: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.518: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.518: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.518: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.518: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.518: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.518: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.518: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.518: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.518: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.518: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.518: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.518: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
06-05 17:43:40.528: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.528: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.528: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.528: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.528: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.528: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.528: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.528: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.528: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.528: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.528: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.528: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.528: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.528: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
06-05 17:43:40.538: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.538: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.538: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.538: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.538: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.538: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.538: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.538: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.538: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.538: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.538: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.538: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.538: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.538: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
06-05 17:43:40.548: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.548: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.548: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.548: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.548: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.548: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.548: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.548: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.548: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.548: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.548: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.548: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.548: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.548: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
我知道我米缺少unregistorReciever的事情,但我應該在哪裏添加
我只想掃描20次,得到Wifi信號的平均RSSI值。除了在onClick – user3659612
上移動wifi.startScan(),我還應該做些什麼其他修正?你能告訴我如何在主要活動中初始化一個數組嗎?之後,我想我可以弄清楚。 – user3659612
它的方式不正確,因爲每次掃描結束並調用onReceive()時,你會運行一個循環20次,所以這會發生像20 * 20次也許,checksoon我會編輯我的答案 – Yazan