【问题标题】:Android - Update Location every x minutesAndroid - 每 x 分钟更新一次位置
【发布时间】:2013-08-24 20:21:11
【问题描述】:

我对 android 很陌生,这是我在这里的第一篇文章,所以请善待! :-)

我正在尝试创建一个在后台运行并每 x 分钟更新一次位置的服务。要每 x 分钟运行一次,我使用的是 AlarmManager,如下所述:Alarm Manager Example

这是我得到的:

package com.example.service1;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;

public class Scheduler extends BroadcastReceiver{

LocationManager locationManager;
LocationListener locationListener;


@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();




    //Code which is executed every X seconds/minutes

    getLocation(context);

    //End of Code
    wl.release();

}

public void setScheduler(Context context) {
    AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Scheduler.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 20, pi);

}


//Method to get the Location

public void getLocation(Context context) {

    Log.e("null","getLocation");
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Log.e(null, "location change");
            makeUseOfLocation(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub

        }

    };

}

//Method to work with the location Data; Instance of Point is created
public void makeUseOfLocation(Location location) {
    Log.e(null,"makeuse");

    Log.e(null,location.getLatitude() + "");
}

}

getLocation() 每 20 秒调用一次,但它永远不会运行 onLocationChanged()(我使用 Eclipse 中的 EmulatorControl 来更改位置)。

我之前使用ScheduledExecutorService 而不是AlarmManager 时遇到了同样的问题。

谁能帮帮我?

【问题讨论】:

    标签: android location alarmmanager repeatingalarm


    【解决方案1】:

    查看您的代码,我从来没有看到将您的侦听器作为参数调用locationManager.requestLocationUpdates()。

    这意味着您的侦听器从未向位置管理器注册,因此不会被调用。

    您可能只想注册一个监听器,而不是每次都注册一个新监听器。

    【讨论】:

      【解决方案2】:

      试试这个..

       protected void updateNotification() {
               LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
               LocationListener locationListener = new MyLocationlistener();
      
               lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, normallocationwait, 0.250f, locationListener);
               location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         }
      
         private class MyLocationlistener implements LocationListener {
      
           public void onLocationChanged(Location location){
               if(location!=null){
                   if(location.hasAccuracy()){
                         dumpLocation(location);
                   }else{
                         dumpLocation(location);
                  }
               } 
           }
      
           public void onProviderDisabled(String provider){
               Log.v("Loc Update","\nProvider disabled: " + provider);
           }
      
           public void onProviderEnabled(String provider){
               Log.v("Loc Update","\nProvider enabled: " + provider);
           }
      
           public void onStatusChanged(String provider, int status, Bundle extras){
               Log.v("Loc Update","\nProvider status changed: " + provider + ", status="
                          + status + ", extras=" + extras);
           }
      

      【讨论】:

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