【发布时间】:2020-02-07 03:47:34
【问题描述】:
我申请了后台服务拍照。在应用程序打开时工作,但在应用程序关闭时不工作。我用设置间隔的警报管理器做到了。
public void onReceive(final Context context, Intent intent) {
// get location
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null){
currentLocation = location;
String pref_nip = SharedPrefManager.getInstance(context).getNip();
push_loc(context,pref_nip,String.valueOf(currentLocation.getLongitude()),String.valueOf(currentLocation.getLatitude()));
}else{
Toast.makeText(context,"yah gagal",Toast.LENGTH_SHORT).show();
}
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context,"Error map",Toast.LENGTH_LONG).show();
}
});
openCamera();
// insert code here
}
private void openCamera() {
CaptureImage captureImage = new CaptureImage();
captureImage.getImage();
}
和
public void startRepeatingBroadcast()
{
ComponentName receiver = new ComponentName(this, MyReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000 * 60 * 2,pi);
}
获取位置后工作。
【问题讨论】:
-
请参考这个答案stackoverflow.com/a/56931642/11128798。它可能会帮助你
标签: java android android-studio alarmmanager