【问题标题】:Android GPS Location PeriodicallyAndroid GPS 定期定位
【发布时间】:2017-11-28 23:12:52
【问题描述】:

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

  1. 我是否应该实施 2 个重复警报管理器,一个用于每 5 分钟,另一个用于时隙。 ?
  2. 或者以某种方式进行,每 5 分钟发出一次火灾警报,并检查它是否在时间段之间,然后只运行定位服务并上传到服务器工作。 ?

请帮助我提出建议/建议。如何在手机电池、效率方面以最佳方式实现这一目标。

【问题讨论】:

  • 在此之前每 5 分钟定位一次是否是个好主意?我不认为手机电池会持续到晚上 9 点
  • @Sanoop 我知道你是对的。这不是一个好的做法,但这是客户的要求。
  • 您好,Google 最近更新了 API,用于以更有效的方式获取用户位置。 android-developers.googleblog.com/2017/06/…这可能对你有帮助。

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


【解决方案1】:

你不应该使用 Handler.postDelayer() 的时间间隔,超过 30 秒,因为它可能会导致内存泄漏。开发几种策略 - 一种用于短时间间隔 - 少于 30 秒,使用 Handler,另一种 - 使用 AlarmManager(用于更长的间隔)。

【讨论】:

  • 我可以使用 Firebase 作业调度程序,上午 9 点的 1 个启动服务,晚上 9 点的另一个停止服务?
  • 我没有使用 Firebase 的作业调度程序,但还有一件事 - 如果您使用 AlarmManager,您应该监听设备重启事件,因为重置设备会取消所有待处理的警报
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在这里,我创建了一个服务,它以 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 {
    
            }
        }
    }
    

    【讨论】:

    • 这对电池使用有什么影响?这会比设置一个 AlarmManager 每分钟触发一次并检查位置更省电吗?
    • 嗨@Nikaoto,是的,它更高效且省电,因为如果您使用警报管理器定期触发请求,它总是会定期检查位置检查。
    【解决方案4】:

    你可以使用处理程序来查询位置,你可以这样做

     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);
    

    【讨论】:

    • 是的,On 表示启用,一个表示禁用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多