我想通过AlarmManager在后台每隔几分钟获取一个位置。以下代码获取一个位置,但似乎打破了AlarmManager机制。我在这附近或咆哮错误的树吗?
public class CarryBoundaryCronnedService extends JobIntentService {
private static final int UNIQUE_JOB_ID=1337;
private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest mLocationRequest;
private LocationCallback mLocationCallback;
private LocationSettingsRequest mLocationSettingsRequest;
private Looper mLooper;
static void enqueueWork(Context ctxt) {
enqueueWork(ctxt, CarryBoundaryCronnedService.class, UNIQUE_JOB_ID,
new Intent(ctxt, CarryBoundaryCronnedService.class));
}
@Override
public void onHandleWork(Intent i) {
Log.d(getClass().getSimpleName(), "onHandleWork.");
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1);
mLocationRequest.setFastestInterval(1);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
mLooper = Looper.myLooper();
mLooper.prepare();
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Log.d(getClass().getSimpleName(), " got a fix");
System.out.println("got a fix");
Looper.myLooper().quitSafely();
}
};
Log.d(getClass().getSimpleName(), " requestLocationUpdates.");
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, mLooper); // enabled by @SuppressWarnings({"MissingPermission"}) above
Log.d(getClass().getSimpleName(), " .requestLocationUpdates");
Log.d(getClass().getSimpleName(), " .invoke os");
Log.d(getClass().getSimpleName(), " loop.");
mLooper.loop();
Log.d(getClass().getSimpleName(), " .loop");
Log.d(getClass().getSimpleName(), " removeLocationUpdates.");
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
Log.d(getClass().getSimpleName(), " .removeLocationUpdates");
Log.d(getClass().getSimpleName(), ".onHandleWork");
}
}
修改 根据注释从AlarmManager计划意图移动到FusedLocationProviderClient计划意图。似乎在棉花糖上工作正常。目前尚不清楚我需要唤醒锁..
public class CarryBoundaryCronnedService extends IntentService {
private final String TAG=getClass().getSimpleName();
CarryBoundaryCronnedService(){
super("CarryBoundaryCronnedService");
}
public void onHandleIntent(Intent i) {
Log.d(TAG, "onHandleIntent.");
LocationResult locationResult = LocationResult.extractResult(i);
if(locationResult==null) {
Log.d(TAG, " no location");
Log.d(TAG, ".onHandleIntent");
return;
}
android.os.PowerManager mgr=(android.os.PowerManager)getSystemService(POWER_SERVICE);
android.os.PowerManager.WakeLock wakeLock;
wakeLock=mgr.newWakeLock(android.os.PowerManager.PARTIAL_WAKE_LOCK, getClass().getSimpleName());
Log.d(TAG, " wakeLock.acqire.");
wakeLock.acquire();
Log.d(TAG, " .wakeLock.acquire");
Log.d(TAG, " got a fix "+locationResult.toString());
Log.d(TAG, " wakeLock.release.");
wakeLock.release();
Log.d(TAG, " .wakeLock.release");
Log.d(TAG, ".onHandleIntent");
}
static void enqueueWork(Context ctxt) {
System.out.println("enqueueWork.");
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(60000);
locationRequest.setFastestInterval(60000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// locationRequest.setNumUpdates(1);
Intent intent=new Intent(ctxt, CarryBoundaryCronnedService.class);
PendingIntent pendingIntent=PendingIntent.getService(ctxt, 0, intent, 0);
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(ctxt);
fusedLocationClient.requestLocationUpdates(locationRequest, pendingIntent); // enabled by @SuppressWarnings({"MissingPermission"}) above
System.out.println(".enqueueWork");
}
}