简体   繁体   中英

Capture location from background and upload that to cloud or save to local storage in android

I want to create an android application which can capture device location even when the app is not running. I had tried to make an app that returns data from service to main thread. That app is working fine when the app is running but if the app is closed it is not capturing any location.

I have attached my service class. Now it is just capturing location and updates the UI. it is working even when the application is in pause . but it is not working when the application is destroyed .

public class DeviceLocationService extends Service {

private LocationListener listener;
private LocationManager locationManager;
private final String TAG = this.getClass().getName();
private int count = 0;

@SuppressLint("MissingPermission")
@Override
public void onCreate() {
    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Intent i = new Intent(Config.LOCATION_INTENT_FILTER);
            HashMap<Object,Object> hashMap = new HashMap<>();
            Log.d(TAG, "onAccuracyChanged: "+location.getAccuracy());

            hashMap.put("latitude",location.getLatitude());
            hashMap.put("longitude",location.getLongitude());
            hashMap.put("accuracy",location.getAccuracy());
            hashMap.put("provider",location.getProvider());

            JSONObject jo = new JSONObject(hashMap);
            i.putExtra("location_data",jo.toString());
            Log.d(TAG, "onLocationChanged: "+jo.toString());
            sendBroadcast(i);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, "onStatusChanged: provider "+provider);
            Log.d(TAG, "onStatusChanged: status "+status);
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d(TAG, "onProviderEnabled: "+provider);
        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    };

    locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    if (locationManager != null) {
        //noinspection MissingPermission
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,listener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,listener);
    }
}

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

}

To run your service in background you need to use foreground service.

add below code in your service

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        startMyOwnForeground();
    else
        startForeground(1, new Notification());
    return START_STICKY;
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(){
    String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
    String channelName = "My Background Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2, notification);
}

and start service with below code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(new Intent(this,DeviceLocationService.class));
    } else
        startService(new Intent(this,DeviceLocationService.class));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM