如果应用程序关闭,后台拍照服务将无法正常工作 (Android Studio)

Take Photo In Background Service Not Working If Apps closed (Android Studio)

我申请了后台拍照功能。在应用程序打开时工作,但在应用程序关闭时不工作。我用闹钟管理器设置了时间间隔。

 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);
}

获取位置时工作。

自从android 8.0更新后就不可能了,你不能在后台使用这种服务而不让用户知道你在做,(通知栏中的一些通知之王) 您可能会发现此 link 有帮助:Create your own accessibility service