【问题标题】:Cannot able to get Location exactly every 10 minutes using Pending Intent and AlarmManager无法使用 Pending Intent 和 AlarmManager 每 10 分钟准确获取一次位置
【发布时间】:2014-07-18 11:57:58
【问题描述】:

我发布了我的需求,问题和代码,我不知道我的代码有什么问题,任何帮助..

我的要求:我想每 10 分钟从 GPS 获取纬度、经度

问题:我一直在获取纬度,经度

我的代码:

1].我在活动中使用此代码,我正在从活动中启动广播接收器

//Pending Intent
Intent in = new Intent(this,GPSReceiver.class);
PendingIntent pi =  PendingIntent.getBroadcast(this, 0, in, 0);
 //Alaram manager   
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
am.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 10*60*1000, pi);

2].BroadCast 类“GPSReceiver.class”,从这里我开始服务名称为 GPSService

public class GPSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service =  new Intent(context,GPSService.class);
        context.startService(service);
    }

}

3].Service Class "GPSService",用于获取位置更新

public class GPSService extends Service {

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

    //Flag to know  GPS Status
    boolean isGPSEnabled = false;
    LocationManager locationmanager;
    String locationgpsprovider = LocationManager.GPS_PROVIDER;

    double latitude;
    double longtitude;

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


        //To get Location service
        locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
        //To get GPS Status
        isGPSEnabled = locationmanager.isProviderEnabled(locationgpsprovider);
        if (!isGPSEnabled) {
            Toast.makeText(this, "Please check your GPS connection", Toast.LENGTH_LONG).show();
        }else {

            locationmanager.requestLocationUpdates(locationgpsprovider, 0, 0, ll);
            }
        return START_NOT_STICKY;
    }


    LocationListener ll = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {


        }

        @Override
        public void onProviderEnabled(String provider) {


        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onLocationChanged(Location location) {

            Log.i("Hei", "10 minutes up");

            latitude = location.getLatitude();
            longtitude = location.getLongitude();

        }
    };

【问题讨论】:

  • 您是否注意到 GPSReceiver 是否被调用?
  • 感谢您的回复...没有调用,有什么原因吗?
  • 嗨,广播只调用一次,有什么想法吗?

标签: android android-activity android-location


【解决方案1】:

每当您发出警报时,它都会向服务传递另一个 Intent,并且位置管理器会重新开始。您不需要在此处设置警报,因为 LocationManager 已经以指定的时间间隔交付位置。

只需启动一次服务并在onCreate 实例化您的位置管理器内容就足够了。在这种情况下,仅使用 onStartCommandreturn START_NOT_STICKY

提示1:定义停止服务的条件,不要让它永远运行。

提示 2:查看requestLocationUpdates javadoc 以设置正确的minTime

提示 3:您可以在 Android 文档中找到更多提示: http://developer.android.com/training/location/index.html

【讨论】:

  • 我想每 10 分钟获取一次位置更新,这就是为什么我使用 Pending Intent 和警报管理器...这是错误的方式吗?..如果是,请告诉我如何实现这个??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多