【问题标题】:How to get device location every 5 minutes using background service in Android?如何在 Android 中使用后台服务每 5 分钟获取一次设备位置?
【发布时间】:2016-11-03 11:52:25
【问题描述】:

我想每 5 分钟获取一次用户位置并显示为 Toast。最好的方法是什么? Service 还是 IntentService?我想在按钮单击时启动和停止服务。怎么做?

【问题讨论】:

    标签: android service geolocation location android-intentservice


    【解决方案1】:

    使用警报服务触发定位服务,这将反过来将位置保存在数据库表中

    public static void setAlarmTimely(Context context) {
        AlarmManager alarmMgr;
        PendingIntent alarmIntent;
    
        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(IntentConstants.ALARM_INTENT, IntentConstants.INTENT_REQUEST_CODE_LOCATION_TRACK);
        alarmIntent = PendingIntent
                .getBroadcast(context, IntentConstants.INTENT_REQUEST_CODE_LOCATION_TRACK, intent, 0);
        alarmMgr.cancel(alarmIntent);
    
        Calendar calendar = Calendar.getInstance();
        LOGD(TAG, time + " ");
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + locationCaptureTime * 60 * 1000,
                5 * 60 * 1000, alarmIntent);
    }
    

    点击按钮取消报警

    AlarmManager alarmMgr;
        PendingIntent alarmIntent;
        LOGD(TAG, "cancelling location update");
        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(IntentConstants.ALARM_INTENT, IntentConstants.INTENT_REQUEST_CODE_LOCATION_TRACK);
        alarmIntent = PendingIntent
                .getBroadcast(context, IntentConstants.INTENT_REQUEST_CODE_LOCATION_TRACK, intent, 0);
        alarmMgr.cancel(alarmIntent);
    

    【讨论】:

      【解决方案2】:

      你可以用LocationManager做到这一点,你需要设置时间间隔。

      例子

      LocationManager = new LocationManager();
       locationManager.requestLocationUpdates(
                              LocationManager.NETWORK_PROVIDER,
                              TIME_BW_UPDATES,
                              MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      

      参数

      provider String: 要注册的提供者的名称

      minTime long:位置更新的最小时间间隔,以毫秒为单位

      minDistance float:位置更新之间的最小距离,以米为单位

      listener LocationListener:一个LocationListener,其

      每次位置更新都会调用onLocationChanged(Location)方法

      您可以在此页面上阅读更多内容 LocationManager

      【讨论】:

        【解决方案3】:

        使用粘性服务来做到这一点

         @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                Log.e(LOG_TAG, "Service Started");
                try {
        
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        
        
                return START_STICKY;
            }
        

        然后从服务的 oncreate 调用这个

           protected void onHandleIntent(Intent intent) {
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //Log.e(LOG_TAG, "Awake - "+i);
                      //  Log.i(LOG_TAG,"TEST ALARM TIME");
                    //  do your task -- here get location 
                    //change the time interval as per your need.
        
                   handler.postDelayed(this, 300000);
                    }
                },1000);
            }
        

        【讨论】:

          猜你喜欢
          • 2018-05-25
          • 1970-01-01
          • 2011-11-09
          • 1970-01-01
          • 2017-09-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多