如果您阅读 android 文档,工作管理器不是安排重复任务的好方法,并且当设备进入睡眠模式时,正在处理的线程工作管理器也会进入睡眠状态以节省电池,请尝试使用处理程序。
https://developer.android.com/reference/android/os/Handler
在我发布的代码中,我使用了 handler 和 firebase jobdispatcher 以 30 秒的固定间隔记录用户位置,即使设备处于睡眠模式也可以工作
//带有处理程序和firebase作业调度程序的文件
public class GetterService extends JobService {
private HandlerThread handlerThread = new HandlerThread("HandlerThread");
private Handler threadHandler;
int delay = 30*1000;
Runnable runnable;
LocationManager locationManager;
@Override
public boolean onStartJob(@NonNull com.firebase.jobdispatcher.JobParameters job) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
handlerThread.start();
threadHandler = new Handler();
threadHandler.postDelayed(runnable= () -> {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location repeatedLocations = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.i("location logger", "getting Longitude as "+ repeatedLocations.getLongitude() +" getting Latitide as "+repeatedLocations.getLatitude());
threadHandler.postDelayed(runnable,delay);
},delay);
return false;
}
@Override
public boolean onStopJob(@NonNull com.firebase.jobdispatcher.JobParameters job) {
return false;
}
}
//带有位置获取器的文件
public class LocationGetter extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
@BindView(R.id.latitudeVal)
TextView latitudeVal;
@BindView(R.id.longitudeVal)
TextView longitudeVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 2);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
}
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher
(new GooglePlayDriver(getApplicationContext()));
Job notificationJob = dispatcher.newJobBuilder()
.setService(GetterService.class)
.setRecurring(true).setTag("jid")
.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
.setTrigger(Trigger.executionWindow(1, 10000))
.setReplaceCurrent(false)
.build();
dispatcher.mustSchedule(notificationJob);
}
@OnClick(R.id.ping)
public void onPingClick() {
}
@Override
public void onLocationChanged(Location location) {
latitudeVal.setText("" + location.getLatitude() + "");
longitudeVal.setText("" + location.getLongitude() + "");
Log.i("dfjh", "" + location.getLongitude() + " " + location.getLatitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}