我是第一次使用 Work Manager,并且已经成功实施。
我每 30 分钟进行一次定位以跟踪员工。
我在第一次同步数据库时启动了我的工作管理器,但我想每天晚上停止它。
这是MyWorker.java
public class MyWorker extends Worker {
private static final String TAG = "MyWorker";
/**
* The desired interval for location updates. Inexact. Updates may be more or less frequent.
*/
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
/**
* The fastest rate for active location updates. Updates will never be more frequent
* than this value.
*/
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2;
/**
* The current location.
*/
private Location mLocation;
/**
* Provides access to the Fused Location Provider API.
*/
private FusedLocationProviderClient mFusedLocationClient;
private Context mContext;
private String fromRegRegCode, fromRegMobile, fromRegGUID, fromRegImei, clientIP;
/**
* Callback for changes in location.
*/
private LocationCallback mLocationCallback;
public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
mContext = context;
}
@NonNull
@Override
public Result doWork() {
Log.d(TAG, "doWork: Done");
//mContext.startService(new Intent(mContext, LocationUpdatesService.class));
Log.d(TAG, "onStartJob: STARTING JOB..");
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(mContext);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
}
};
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
try {
mFusedLocationClient
.getLastLocation()
.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful() && task.getResult() != null) {
mLocation = task.getResult();
String currentTime = CommonUses.getDateToStoreInLocation();
String mLatitude = String.valueOf(mLocation.getLatitude());
String mLongitude = String.valueOf(mLocation.getLongitude());
LocationHistoryTable table = new LocationHistoryTable();
table.setLatitude(mLatitude);
table.setLongitude(mLongitude);
table.setUpdateTime(currentTime);
table.setIsUploaded(CommonUses.PENDING);
LocationHistoryTableDao tableDao = SohamApplication.daoSession.getLocationHistoryTableDao();
tableDao.insert(table);
Log.d(TAG, "Location : " + mLocation);
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
/**
* Upload on server if network available
*/
if (Util.isNetworkAvailable(mContext)) {
checkForServerIsUP();
}
} else {
Log.w(TAG, "Failed to get location.");
}
}
});
} catch (SecurityException unlikely) {
Log.e(TAG, "Lost location permission." + unlikely);
}
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
null);
} catch (SecurityException unlikely) {
//Utils.setRequestingLocationUpdates(this, false);
Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
}
return Result.success();
}
}
启动 Worker 的代码:
PeriodicWorkRequest periodicWork = new PeriodicWorkRequest.Builder(MyWorker.class, repeatInterval, TimeUnit.MINUTES)
.addTag("Location")
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("Location", ExistingPeriodicWorkPolicy.REPLACE, periodicWork);
每天晚上有什么特别的方法可以阻止它吗?
您的帮助将不胜感激。