我想在我的手机中每 5 秒检查一次可用的无线网络。
所以我为我的应用程序编写了这段代码,但这段代码每 15 秒检查一次 WIFI 网络!
这是一项服务,每 15 秒扫描一次 WIFI 网络,如果发现没有密码的 WIFI 会振动手机并播放声音...
如何每 5 或 3 秒检查一次无线网络?
package com.sirbl4ck.wififinder;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Service;
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.IBinder;
import android.os.Vibrator;
import android.os.PowerManager;
import android.widget.Toast;
import android.media.MediaPlayer;
public class MyService extends Service {
WifiManager mainWifiObj;
WifiScanReceiver wifiReciever;
PowerManager pm;
PowerManager.WakeLock wl;
public MyService() {
}
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"Log");
wl.acquire();
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
mainWifiObj.startScan();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Toast.makeText(this, "WIFI Finder Service Started", Toast.LENGTH_LONG).show();
}
public void onDestroy() {
wl.release();
Toast.makeText(this, "WIFI Finder Service Stoped", Toast.LENGTH_LONG).show();
}
public void alert(){
final MediaPlayer als = MediaPlayer.create(this, R.raw.alertm);
als.start();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
for(int iss=0;iss<wifiScanList.size();iss++){
String uug=wifiScanList.get(iss).toString();
if(uug.contains("WPA") && !uug.contains("WEP")){
v.vibrate(1500);
alert();
Toast.makeText(MyService.this, "WIFI Found !", Toast.LENGTH_LONG).show();
}
}
}
}
}