Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android RequestLocationUpdate使用带BroadcastReceiver的PendingEvent_Android - Fatal编程技术网

Android RequestLocationUpdate使用带BroadcastReceiver的PendingEvent

Android RequestLocationUpdate使用带BroadcastReceiver的PendingEvent,android,Android,我如何使用 requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent) 在广播接收器中,这样我就可以不断获得GPS坐标 我是否必须为LocationListener创建单独的类 我的项目目标是当我收到BOOT_COMPLETED时,开始定期获取

我如何使用

requestLocationUpdates(long minTime, float minDistance, Criteria criteria,
                                                           PendingIntent intent) 
在广播接收器中,这样我就可以不断获得GPS坐标

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

我的项目目标是当我收到
BOOT_COMPLETED
时,开始定期获取GPS LAT和LONG

我试过的代码是:

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);
     }*/
}

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

  • 使用您正在注册的方法,注册一个
    BroadcastReceiver
    ,该接收器具有一个与您的
    pendingent
    (2.3+)中保存的意图相匹配的意图过滤器,或者,如果您只对单个位置提供程序感兴趣,
    requestLocationUpdates(字符串提供程序、long minTime、float minDistance、pendingent intent)
    (1.5+)
  • 使用
    requestLocationUpdates(字符串提供程序、long-minTime、float-minDistance、LocationListener-listener)的
    LocationManager
    方法注册
    LocationListener
  • 我认为您有点困惑,因为您可以使用
    BroadcastReceiver
    LocationListener
    来处理位置更新—您不需要两者都使用。注册更新的方法非常相似,但您接收更新的方式确实非常不同

    BroadcastReceiver
    将允许您的应用程序/服务在当前未运行时被唤醒。在服务未运行时关闭服务将显著减少您对用户电池的影响,并将任务杀手应用程序终止服务的机会降至最低

    LocationListener
    将要求您保持服务运行,否则您的
    LocationListener
    将在您的服务关闭时死亡。如果您使用这种方法,任务杀手应用程序可能会极大地损害您的服务


    根据您的问题,我怀疑您需要使用
    BroadcastReceiver
    方法。

    我想我有点困惑。我添加了尝试使用的代码。这有什么问题吗?您的
    LocationListener
    需要使用
    服务,该服务必须永久保持活动状态,否则您将无法收到任何更新。正如我在回答中所说,您最好使用另一种方法来处理位置更新,方法是注册一个
    pendingent
    ,这将导致每次位置更新时发送一个广播来唤醒您的
    BroadcastReceiver
    类中的ut GPS代码作为活动(包括locationlistener)。在broadcastreceiver中,将该意图设置为挂起意图。我说得对吗?你有例子吗?谢谢。没有,如果你使用的是
    pendingent
    /
    broadcastreceiver
    机制,你不需要
    LocationListener
    。你的
    broadcastreceiver
    做两件事:1.设备启动时启动一切。2.哈哈NDL将更新后续的位置。我之前的评论中的链接中有一个示例。您可能希望接受下面的答案。
    public class MobileViaNetReceiver extends BroadcastReceiver {
    
        private static final String TAG = "MobileViaNetReceiver"; // please
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
                Log.i(TAG, "Boot : registered for location updates");
                LocationManager lm = (LocationManager) context
                                    .getSystemService(Context.LOCATION_SERVICE);
                Intent intent = new Intent(context, this.getClass());
                PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
                                            PendingIntent.FLAG_UPDATE_CURRENT);
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,5,pi);
            } else {
                String locationKey = LocationManager.KEY_LOCATION_CHANGED;
                if (intent.hasExtra(locationKey)) {
                    Location loc = (Location) intent.getExtras().get(locationKey);
                    Log.i(TAG, "Location Received");
                    try {
                        DbAdapter_GPS db = new DbAdapter_GPS(context);//what's this
                        db.open();
                        String android_id = Secure.getString(
                            context.getContentResolver(), Secure.ANDROID_ID);
                        Log.i(TAG, "Android id is :" + android_id);
                        db.insertGPSCoordinates(android_id,
                            Double.toString(loc.getLatitude()),
                            Double.toString(loc.getLongitude()));
                    } catch (Exception e) { // NEVER catch generic "Exception"
                        Log.i(TAG, "db error catch :" + e.getMessage());
                    }
                }
            }
        }
    }