我正在开发一个应用程序,它可以使用预定义的时间(使用警报管理器)搜索某个Wi-Fi SSID并在找到时使电话静音。我希望我的应用程序能够在后台运行,因此我为Wi-Fi搜索提供了服务等级。另外,我创建了一个类,可以检测是否找到了特定的Wi-Fi(在范围内)。我将Wi-Fi检测类放在服务类中。
问题:
当应用程序在后台运行时(它成功搜索并检测到Wi-Fi),服务正常工作,但当我关闭应用程序时,服务正常工作(服务已启动),但Wi-Fi检测器类无法检测到任何Wi -Fi就好像没有Wi-Fi在范围内。 我不知道为什么会出现这个问题,我希望有人不能帮助我。
这是我服务的代码:
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.media.AudioManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
import java.util.List;
/**
* Created by Msi on 1/6/2018.
*/
public class MyService extends Service{
AudioManager audio;
String wifis[];
WifiManager mainWifiObj;
WifiScanReceiver wifiReciever;
String accesspoint = "Silent_Area";
public boolean founded = false;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {//this is invoked first
super.onCreate();
Toast.makeText(this,"serivce is created",Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {//work is done here
mainWifiObj = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Toast.makeText(this,"serivce is started",Toast.LENGTH_LONG).show();
if(!mainWifiObj.isWifiEnabled()){
mainWifiObj.setWifiEnabled(true);
}
System.out.println("-=--=-=RECIEVED Silencer-=--=-=");
final int[] duration = {0};
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (duration[0] < 15 * 60 * 1000) {
while (duration[0] < 7 * 60 * 1000){
System.out.println("thread Searches every 5 sec");
mainWifiObj.startScan();
if (founded == true) {
System.out.println("found");
audio.setRingerMode(1);
}
if (founded == false) {
System.out.println("didnt find");
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
System.out.println("hello keep searching ");
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
duration[0] += 5 * 1000;
}
System.out.println("thread searches every 30 sec");
mainWifiObj.startScan();
if (founded == true) {
System.out.println("found");
audio.setRingerMode(1);
}
if (founded == false) {
System.out.println("didnt find");
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
System.out.println("hello keep searching ");
try {
Thread.sleep(1000 * 30);
} catch (InterruptedException e) {
e.printStackTrace();
}
duration[0] += 30 * 1000;
}
System.out.println("15 min has passed");
stopSelf();
}
});
thread.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {//to destroy
Toast.makeText(this,"serivce is stopped",Toast.LENGTH_LONG).show();
super.onDestroy();
stopSelf();
}
public class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
wifis = new String[wifiScanList.size()];
for (int i = 0; i < wifiScanList.size(); i++) {
wifis[i] = ((wifiScanList.get(i)).toString());
}
String filtered[] = new String[wifiScanList.size()];
int counter = 0;
final AudioManager manual = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
for (String eachWifi : wifis) {
String[] temp = eachWifi.split(",");
filtered[counter] = temp[0].substring(5).trim();
counter++;
System.out.println(/*temp[0].substring(5).trim()*/temp[0]+temp[1]+temp[2]+temp[3]+temp[4]+temp[5]);
if (accesspoint.equals(temp[0].substring(5).trim())) {
System.out.println("== Founded ==");
founded = true;
break;
}
else{
founded=false;
}
}
}
}
}
答案 0 :(得分:-1)
尝试扩展IntentService而不是Service或创建ServiceHandler
两者的例子都在API指南中:
https://developer.android.com/guide/components/services.html