user6498197
user6498197

Reputation:

Background service always running

I have some code to create background service to working always.
this service will be working after app destroyed.
I test more than 20 code example and this code working in simulator, but not working in my phone marshmallow android 6.

This is service file for my app

public class locationService extends Service implements LocationListener {
    private LocationManager _locMgr;
    com.parse.groupbox.tools.locationTools loc;
    Random RND = new Random();
    Timer timer = new Timer();

    @Override
    public void onCreate() {
        super.onCreate();
        loc = new com.parse.groupbox.tools.locationTools(this);
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        try {
            gpsSetup();
            timer.cancel();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    updateGps();
                }
            }, 5000, 10);
        } catch (Exception ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            gpsSetup();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    updateGps();
                }
            }, 5000, 10);
        } catch (Exception ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
        Toast.makeText(this, "start", Toast.LENGTH_SHORT).show();
        return Service.START_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        try {
            gpsSetup();
            timer.cancel();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    updateGps();
                }
            }, 5000, 10);
        } catch (Exception ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
        Toast.makeText(this, "task service start", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            gpsSetup();
            timer.cancel();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    updateGps();
                }
            }, 5000, 10);
        } catch (Exception ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
        Toast.makeText(this, "service destroyed", Toast.LENGTH_SHORT).show();
    }

    public void gpsSetup() {
        try {
            _locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria locationCriteria = new Criteria();
            locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);
            locationCriteria.setPowerRequirement(Criteria.POWER_MEDIUM);
            String locationProvider = _locMgr.getBestProvider(locationCriteria, true);
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            _locMgr.requestLocationUpdates(locationProvider, 1, 1, this);

            Toast.makeText(this, "start service", Toast.LENGTH_SHORT).show();
        }
        catch (Exception Ex)
        {
            Toast.makeText(this, Ex.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    public void updateGps(){
        try{
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            String locationProvider = LocationManager.NETWORK_PROVIDER;
            _locMgr.requestLocationUpdates(locationProvider, 1, 1, this);
        }catch (Exception Ex){
            Toast.makeText(this, Ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        int result = RND.nextInt(100);
        if(result > 30){
            try{
                loc.LocationSyncToServer(location);
                Toast.makeText(this, "location", Toast.LENGTH_LONG).show();
            }catch (Exception Ex){
                Toast.makeText(this, Ex.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

Please read my codes and give me answer to solve it.

This is the code to run the service:

Intent background = new Intent(getApplicationContext(),   com.parse.groupbox.services.locationService.class);
startService(background);

Upvotes: 2

Views: 86

Answers (2)

Salman
Salman

Reputation: 2471

Android M introduced Doze and App Standby which limits background services in order to preserve battery. How to handle Doze can be found in here android developer site.

Upvotes: 0

Praveen
Praveen

Reputation: 916

try this

put this code where you want to stop service

Intent intent = new Intent(getApplicationContext(),com.parse.groupbox.services.locationService.class);
intent.setAction(Constants.ACTION.STOPTFOREGROUND_ACTION);
stopService(intent);

in your Service onStartCommand()

if(intent.getAction()==Constants.ACTION.STOPTFOREGROUND_ACTION){
 stopForeground(true);
 stopSelf();
 }

create a class Constants

public class Constants {
public interface ACTION {
  String STOPFOREGROUND_ACTION = "com.package.packageName.action.stopforeground";
   }
 }

Upvotes: 2

Related Questions