我正在尝试制作一个将所有wifi BSSID和RSS存储到SQLite数据库中的应用程序。 (我的SQLite表名为“scanres”
这是我onCreate()
上的代码的一部分:
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); //jgn lupa manifest xml nya kasih permission!!!
if (wifi.isWifiEnabled() == false)
{
wifi.setWifiEnabled(true);
}
x = new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
results = wifi.getScanResults();
size = results.size();
if (size > 0) {
for (int i=0; i<size; i++){
ScanResult scanresult = wifi.getScanResults().get(i);
int rssi = scanresult.level;
String bssid = scanresult.BSSID;
datavar.execSQL("insert into scanres values('"+bssid+"',"+rssi+")");
}
unregisterReceiver(x); //stops the continuous scan
} else {
unregisterReceiver(x);
Toast.makeText(SearchActivity.this, "FAIL", Toast.LENGTH_LONG).show();
}
}
};
//wifiscan start
new wifiscan1().execute();
这是我的wifiscan1,我正在使用AsyncTask:
public class wifiscan1 extends AsyncTask<Void, String, Void>{
ProgressDialog pd = new ProgressDialog (SearchActivity.this);
protected void onPreExecute(){
pd.setMessage("wifi scan...");
pd.setIndeterminate(true);
pd.show();
}
protected void onPostExecute(Void result){
pd.dismiss();
}
@Override
protected Void doInBackground(Void... params){
registerReceiver(x, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();
return null;
}
}
我想让这个应用程序连续扫描12次。我尝试使用for loop
,但是在当前扫描完成之前,下一次扫描已经开始,导致跳过当前扫描。
如何在上一次扫描完成后启动下一次扫描?
更新:我的logcat
05-08 16:10:42.869: E/Database(15427): at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:11:00.919: E/Database(15427): at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:15:19.239: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:152)
05-08 16:15:19.239: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.249: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:153)
05-08 16:15:19.249: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.269: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:152)
05-08 16:15:19.269: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.269: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:153)
05-08 16:15:19.269: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.289: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:152)
05-08 16:15:19.289: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.299: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:153)
05-08 16:15:19.299: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.309: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:152)
05-08 16:15:19.309: E/Database(15682): at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.329: E/Database(15682): at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:15:19.819: E/AndroidRuntime(15682): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@40573ba8
05-08 16:15:19.819: E/AndroidRuntime(15682): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.arranda.insidefek.SearchActivity$wifiscan1$1@40573ba8
05-08 16:15:19.819: E/AndroidRuntime(15682): at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:174)
05-08 16:15:26.099: E/AndroidRuntime(15700): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@4054f2d8
05-08 16:15:26.099: E/AndroidRuntime(15700): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.arranda.insidefek.SearchActivity$wifiscan1$1@4055b3a0
05-08 16:15:26.099: E/AndroidRuntime(15700): at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:174)
05-08 16:15:33.499: E/AndroidRuntime(15711): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@40541cd0
05-08 16:15:33.499: E/AndroidRuntime(15711): at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:169)
05-08 16:15:40.329: E/AndroidRuntime(15721): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@40559ae8
05-08 16:15:40.329: E/AndroidRuntime(15721): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.arranda.insidefek.SearchActivity$wifiscan1$1@40559ae8
05-08 16:15:40.329: E/AndroidRuntime(15721): at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:174)
05-08 16:16:23.159: E/AndroidRuntime(15792): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@40559988
05-08 16:16:23.159: E/AndroidRuntime(15792): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.arranda.insidefek.SearchActivity$wifiscan1$1@40559988
05-08 16:16:23.159: E/AndroidRuntime(15792): at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:173)
05-08 16:18:01.519: E/Database(15901): at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:18:02.559: E/AndroidRuntime(15901): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$1@40532608
05-08 16:18:02.559: E/AndroidRuntime(15901): at com.arranda.insidefek.SearchActivity$1.onReceive(SearchActivity.java:86)
05-08 16:18:09.689: E/Database(15915): at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:18:09.919: E/AndroidRuntime(15915): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$1@40532650
05-08 16:18:09.919: E/AndroidRuntime(15915): at com.arranda.insidefek.SearchActivity$1.onReceive(SearchActivity.java:86)
答案 0 :(得分:0)
在onPostExecute()
的{{1}}中,检查是否达到了所需的结果。如果您想再次运行该任务,请使用AsyncTask
方法再次调用AsyncTask
。
例如:
onPostExecute()
当int i = 0;
public class wifiscan1 extends AsyncTask<Void, String, Void>{
ProgressDialog pd = new ProgressDialog (SearchActivity.this);
protected void onPreExecute(){
pd.setMessage("wifi scan...");
pd.setIndeterminate(true);
pd.show();
}
protected void onPostExecute(Void result){
pd.dismiss();
if(result){
if(i < 12) {
new wifiscan1().execute();
}
}
}
@Override
protected Void doInBackground(Void... params){
registerReceiver(x, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();
return null;
}
}
在后台线程中运行时,在主UI线程中运行的进程无法控制后台线程的工作。因此,一旦完成,您就必须再次调用AsyncTasks
实例。
答案 1 :(得分:0)
wifi.startScan()
放在onRecieve()
方法上,并在名为count
的整数的帮助下
所以我的代码现在看起来像这样:
x = new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
datavar.execSQL("delete from hasilscan");
results = wifi.getScanResults();
size = results.size();
if (size > 0) {
for (int i=0; i<size; i++){
ScanResult scanresult = wifi.getScanResults().get(i);
int rssi = scanresult.level;
String bssid = scanresult.BSSID;
datavar.execSQL("insert into hasilscan values('"+bssid+"',"+rssi+")");
}
count++;
if (count < 12) { //because i want the wifi scan to be repeated 12 times in a row
wifi.startScan();
} else {
unregisterReceiver(x); //stops the continuous scan
}
} else {
unregisterReceiver(x);
Toast.makeText(MainActivity.this, "FAIL", Toast.LENGTH_LONG).show();
}
}
};
使用此代码,新扫描在上一次扫描完成后启动,因此不会中断上一次扫描