我正在尝试在后台运行一个应用程序,它会告诉我 wifi 的状态何时被修改;
public class BackgroundJobs extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("BACKJOBS", "here we are");
wifiInfo wifiHandler = new wifiInfo(this);
// wifistatus checks if wifi is on or off, returning a boolean
if(wifiHandler.wifiStatus())
Log.i("BACKJOBS", "WIFI ON");
else
Log.i("BACKJOBS", "WIFI OFF");
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
}
在我的 MainActivity 中,我按如下方式启动服务:
startService(new Intent(this, BackgroundJobs.class));
在 AndroidManifest.xml 中:
<service android:name=".gestioneServizi.BackgroundJobs"/>
我启动我的应用程序,按下主页按钮回到家,打开或关闭 wifi,但我没有看到日志“BACKJOBS”。
我是误解了什么还是我使用了错误的方法?