Android requestLocationUpdates使用PendingIntent和BroadcastReceiver

use*_*844 3 android

我该怎么用

requestLocationUpdates(long minTime, float minDistance, Criteria criteria,
                                                           PendingIntent intent) 
Run Code Online (Sandbox Code Playgroud)

在BroadcastReciver中,我可以继续获取GPS坐标.

我是否必须为LocationListener创建一个单独的类?

我的项目的目标是当我收到BOOT_COMPLETED开始定期获得GPS拉特和长期时.

我试过的代码是:

public class MobileViaNetReceiver extends BroadcastReceiver {

    LocationManager locmgr = null;
    String android_id;
    DbAdapter_GPS db;

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            startGPS(context);
        } else {
            Log.i("MobileViaNetReceiver", "Received unexpected intent "
                + intent.toString());
        }
    }

    public void startGPS(Context context) {
        Toast.makeText(context, "Waiting for location...", Toast.LENGTH_SHORT)
                .show();
        db = new DbAdapter_GPS(context);
        db.open();
        android_id = Secure.getString(context.getContentResolver(),
                Secure.ANDROID_ID);
        Log.i("MobileViaNetReceiver", "Android id is _ _ _ _ _ _" + android_id);
        locmgr = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5,
                onLocationChange);
    }

    LocationListener onLocationChange = new LocationListener() {

        public void onLocationChanged(Location loc) {
            // sets and displays the lat/long when a location is provided
            Log.i("MobileViaNetReceiver", "In onLocationChanged .....");
            String latlong = "Lat: " + loc.getLatitude() + " Long: "
                + loc.getLongitude();
            // Toast.makeText(this, latlong, Toast.LENGTH_SHORT).show();
            Log.i("MobileViaNetReceiver", latlong);
            try {
                db.insertGPSCoordinates(android_id,
                        Double.toString(loc.getLatitude()),
                        Double.toString(loc.getLongitude()));
            } catch (Exception e) {
                Log.i("MobileViaNetReceiver",
                        "db error catch _ _ _ _ " + e.getMessage());
            }
        }

        public void onProviderDisabled(String provider) {}

        public void onProviderEnabled(String provider) {}

        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };    
    //pauses listener while app is inactive
    /*@Override
    public void onPause() {
        super.onPause();
        locmgr.removeUpdates(onLocationChange);
    }

    //reactivates listener when app is resumed
    @Override
    public void onResume() {
        super.onResume();
        locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, onLocationChange);
     }*/
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*son 6

有两种方法可以做到这一点:

  1. 使用您所使用的方法并注册一个BroadcastReceiver具有与您PendingIntent(2.3+)中所包含的Intent匹配的intent过滤器,或者如果您只对单个位置提供者感兴趣,则注册一个requestLocationUpdates (String provider, long minTime, float minDistance, PendingIntent intent)(1.5+).
  2. LocaltionListener使用requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)方法注册一个LocationManager.

我认为你开始有点困惑,因为你可以处理使用位置更新或者一个BroadcastReceiver 一个LocationListener-你不需要两者.注册更新的方法非常相似,但是如何接收它们实际上是非常不同的.

A BroadcastReceiver将允许您的应用/服务被唤醒,即使它当前没有运行.在服务未运行时关闭服务将大大降低您对用户电池的影响,并最大程度地降低Task Killer应用程序终止服务的可能性.

LocationListener遗嘱要求您保持服务正常运行,否则您LocationListener的服务将在您的服务关闭时死亡.如果您使用此方法,您可能会冒着极端偏见杀死任务杀手应用程序的风险.

从您的问题,我怀疑您需要使用该BroadcastReceiver方法.

  • 不,如果你使用`PendingIntent` /`BroadcastReceiver`机制,你不需要`LocationListener`.你的`BroadcastReceiver`做了两件事:1.在设备启动时启动所有操作.2.处理后续位置更新.我之前的评论中的链接中有一个示例. (2认同)