dfritsi
dfritsi

Reputation: 1232

Android location updates in a Service

I want to do something like this:

I have two major problems:

  1. How can I detect that my application goes to background? I haver several activities, and I tried that in my MainActivity overriding onPause, but the onPause is also called when I start an other activity.

  2. This problem is more important: How should my Service which checks for my location look like? I tried several approaches, but no success.

My Service looks like this, and it's not working. What should I change to make it work?

package com.pivoscore.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

public class LocationService extends Service {

    private LocationListener locationListener;

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

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        super.onStartCommand(intent, flags, startId);
        return Service.START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        this.locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this.locationListener);
    }

    private static class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(final Location location) {
        }

        @Override
        public void onProviderDisabled(final String provider) {
        }

        @Override
        public void onProviderEnabled(final String provider) {
        }

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

    }

}

Upvotes: 9

Views: 14737

Answers (2)

s.d
s.d

Reputation: 29436

  1. Application is not a visual component in Android. It is divided into Activities, each of them run when visible, paused and destroyed otherwise. So, there is no concept of whole Application going to background, Activities are paused and resumed on the basis of their individual visibility and are completely independent of other Activities in this matter.

  2. Your Service shall register with Location Manager in its onCreate(), unregister from the same in its onDestroy(). In its onBind() it shall return a Messenger object. And, in onLocationChanged() it should send a message through its shared Messenger. No need to use START_STICKY as you don't want Service running all the time.

  3. The Activity (can be any activity in the App) just needs to call bindService() in its onStart(), The service will start if not already, and Activity will get a Messenger from service. Also, Activity should call unbindService() from its onStop(). The Service will automatically stop when nothing is bound to it.

  4. If you need to do the stuff in point 3 at App (Task) level, implement the Application class, and use its onCreate() and onTerminate(). Application class is not paused or stopped like an Activity.

Upvotes: 2

itsrajesh4uguys
itsrajesh4uguys

Reputation: 4638

Try This code.. By using this you can find whether your application is in foreground or background. Hope this will help you.

try {
    foreground = new ForegroundCheckTask().execute(ctx).get();
}

======================================


class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

@Override
protected Boolean doInBackground(Context... params) {
    final Context context = params[0].getApplicationContext();
    return isAppOnForeground(context);
}

private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager
            .getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }
    final String packageName = context.getPackageName();


    String activePackageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
    if (activePackageName.equals(packageName)) {
        return true;
    }
    else{
        return false;
    }


}

}

Upvotes: 0

Related Questions