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

Issue

I made an application to take photos in the background service. works when the application opens but does not work when the application is closed. I made it with the alarm manager with a set interval.

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

and

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

work when get location.

Solution

it’s not possible since the android 8.0 update, you cannot use this kind of service in background without letting the user know that you are doing it, (some king of notification in the notification bar)
you may find this link helpful: Create your own accessibility service

Answered By – אביב נח

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published