即使应用关闭,如何每隔5秒将用户位置数据发送到服务器?

时间:2018-12-27 09:02:56

标签: android location

即使Android中的应用已关闭,如何使用Restful API每五秒钟将用户位置数据发送到服务器?

请帮助我

2 个答案:

答案 0 :(得分:0)

如果要使代码在关闭应用程序后仍运行,则需要使用服务,即使关闭应用程序,服务也可以在后台运行,并且您可能需要使用带有该服务的广播接收器来保持每次完成时运行它。

这是服务:

public class myService extends Service {

    public static int counter = 0;
    public myReceiver myReceiver = new myReceiver();

    @Override
    public void onCreate() {
        super.onCreate();
        //this line register the Receiver for the first time
        myService.this.registerReceiver(myReceiver, new IntentFilter("com.example.myApp"));

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //Here you have to put the code that gets the location and send it

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    //here you sent a broadcast message to start the reciever
    //note that the broadcast message that you send has to be unique writing you package name will be fine ex: com.example.myApp
        Intent sendBroadCast = new Intent("com.example.myApp");
        sendBroadcast(sendBroadCast);
    }

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

这是广播接收器:

公共类myReceiver扩展了BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        if("com.example.myApp".equals(intent.getAction())){
           //the handler is used as a timer here
           Handler handler = new Handler();
           handler.postDelayed(new Runnable() {
               @Override
               public void run() {
                   Intent myServ = new Intent(context, myService.class);
                   try {
                       context.startService(myServ);
                   }catch (Exception e){

                   }
               }
           },5000);
        }
    }
}

答案 1 :(得分:0)

您可以创建一个后台服务,该服务在用户锁定屏幕或从后台关闭您的应用程序时可以使用 您必须通过以下方式创建服务: 首先创建这样的Service类:

public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks {


public static double latitude;
public static double longitude;

private int retryGPS = 0;
private int retryNetwork = 0;
private Handler handler;
private Runnable runnable;
private GoogleApiClient mGoogleApiClient;
private LocationManager mLocationManager;
private LocationListener[] mLocationListeners = new LocationListener[]{
        new LocationListener(LocationManager.GPS_PROVIDER),
};

private static final int LOCATION_INTERVAL = 0;
private static final float LOCATION_DISTANCE = 1;

private static final String TAG = "LocationService";

@Override
public void onCreate() {

    buildGoogleApiClient();

    initializeLocationManager();

    locationRequest();

    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            sendLocation();
        }
    };

    sendLocation();
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .build();
}

private void initializeLocationManager() {

    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    }
}

private void locationRequest() {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    mLocationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
            mLocationListeners[0]);
}


private void sendLocation() {

        //TODO: you can use location here
       handler.postDelayed(runnable,5000);
}

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

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    if (!mGoogleApiClient.isConnected())
        mGoogleApiClient.connect();
    return START_STICKY;
}

@Override
public void onConnected(Bundle bundle) {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    if (location != null) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    } else {
        try {
            Thread.sleep(3000);
            onConnected(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onDestroy() {

    handler.removeCallbacks(runnable);

    if (mLocationManager != null) {
        for (LocationListener mLocationListener : mLocationListeners) {
            try {
                mLocationManager.removeUpdates(mLocationListener);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    super.onDestroy();
}

private class LocationListener implements android.location.LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {

    Location mLastLocation;

    public LocationListener(String provider) {

        Log.d(TAG, "LocationListener: " + provider);
        mLastLocation = new Location(provider);
    }

    @Override
    public void onLocationChanged(final Location location) {

        mLastLocation.set(location);
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Log.d(TAG, "onLocationChanged: { latitude: " + latitude + " ,longitude: " + longitude + " , accuracy: " + location.getAccuracy() + " }");
    }

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

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

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

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    }

}

}

然后在清单中注册服务

 <service
        android:name=".service.LocationService"
        android:enabled="true"
        android:process=":process" />

然后从任何活动或片段开始服务:

public static void mStopService(Context context) {
    context.stopService(new Intent(context, LocationService.class));
}

public static void mStartService(Context context) {
    context.startService(new Intent(context, LocationService.class));
}