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


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>() {
        public void onSuccess(Location location) {
            if (location != null){
                currentLocation = location;
                String pref_nip = SharedPrefManager.getInstance(context).getNip();
                Toast.makeText(context,"yah gagal",Toast.LENGTH_SHORT).show();
    task.addOnFailureListener(new OnFailureListener() {
        public void onFailure(@NonNull Exception e) {
             Toast.makeText(context,"Error map",Toast.LENGTH_LONG).show();
    // insert code here

private void openCamera() {
    CaptureImage captureImage = new CaptureImage();


public void startRepeatingBroadcast()
    ComponentName receiver = new ComponentName(this, MyReceiver.class);
    PackageManager pm = this.getPackageManager();


    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.


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