Android GPS定位

时间:2017-06-26 06:37:25

标签: android alarmmanager android-gps android-intentservice repeatingalarm

我想创建一个应用程序,它在上午9:00到晚上9:00之间每5分钟获取一次用户的位置。现在我无法思考流程。 我很困惑

  1. 我应该实现2个重复的警报管理器,每5分钟一个,另一个用于时隙。 ?
  2. 或者以某种方式进行,每5分钟发出一次火警并检查它是否在时隙之间,然后只运行位置服务并上传到服务器工作。 ?
  3. 请帮我提出建议/建议。如何在手机电池,效率方面实现最佳方法。

4 个答案:

答案 0 :(得分:0)

那么你可以使用处理程序来查询位置,你可以做这样的事情

 Handler handler = new Handler();
 Runnable runnable = new Runnable() {
        public void run() {
            currentLocation = getCurrentUserLocation();
            handler.postDelayed(this, 1000*60*5);
        }
    }
 };
 handler.postDelayed(runnable, 1000*60*5);

为了检查时间间隔,可以为此任务设置一个报警管理器,

一旦达到时间限制,您需要通过

删除处理程序回调
handleLocation.removeCallbacksAndMessages(null);

答案 1 :(得分:0)

您不应该使用Handler.postDelayer()来超过30秒的时间间隔,因为它可能会导致内存泄漏。制定一些策略 - 一个用于短时间间隔 - 少于30秒,使用Handler和另一个 - 使用AlarmManager(更长的间隔)。

答案 2 :(得分:0)

参考https://codelabs.developers.google.com/codelabs/background-location-updates-android-o/index.html?index=..%2F..%2Findex#0

您可以在位置请求中将更新间隔设置为5分钟。它与Android" O"兼容还

答案 3 :(得分:0)

在这里,我创建了一个服务,以1分钟的间隔给出当前位置。您可以根据您的要求修改它 -

还要在gradle文件中添加它 -

compile 'com.google.android.gms:play-services:9.4.0'

LocationService.class

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.text.SimpleDateFormat;
import java.util.Date;

public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {
    protected static final String TAG = "location-updates";
    private static final long day = 24 * 60 * 60 * 1000;
    private static final long hours = 60 * 60 * 1000;
    private static final long minute = 60 * 1000;
    private static final long fiveSec = 5 * 1000;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;

    @Override
    public void onCreate() {
        super.onCreate();
        mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(minute);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setSmallestDisplacement(0);

        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        if (mGoogleApiClient != null) {
            mGoogleApiClient.connect();
        }
    }


    @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;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }


    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
    }

    //Location Listener
    @Override
    public void onLocationChanged(final Location location) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String currentDateandTime = sdf.format(new Date());
        Log.e("Location ", location.getLatitude() + " " + location.getLongitude() + " " + currentDateandTime);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

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


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


    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        boolean isGranted = false;
        for (int i = 0; i < grantResults.length; i++)
            if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION) && (grantResults[i] == PackageManager.PERMISSION_GRANTED))
                isGranted = true;
        if (isGranted) {
            startService(new Intent(this, LocationService.class));

        } else {

        }
    }
}