Reputation: 1232
I want to do something like this:
When my application starts I want to start a Service which should check my location
When the application goes to background I want to stop the service
I have two major problems:
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.
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
Reputation: 29436
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.
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.
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.
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
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