0

locpoll 在一段时间(25 秒)内获取位置,似乎有时它会停止发送位置更新或接收器停止接收。这是我的代码。有什么问题吗。。

public class LocationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle b = intent.getExtras();
            Location loc = (Location) b.get(LocationPoller.EXTRA_LOCATION);
            String msg;
            if (loc == null) {
                loc = (Location) b.get(LocationPoller.EXTRA_LASTKNOWN);
                if (loc == null) {
                    msg = intent.getStringExtra(LocationPoller.EXTRA_ERROR);
                    cPosicion.latitud = 0;
                    cPosicion.longitud = 0;
                    cPosicion.timeStamp = System.currentTimeMillis();
                } else {
                    msg = "TIMEOUT, lastKnown=" + loc.toString();
                    Log.e("e_timeout", "timeout");
                    cPosicion.latitud = 0;
                    cPosicion.longitud = 0;
                    cPosicion.timeStamp = System.currentTimeMillis();
                }
            } else {
                msg = loc.toString();
                cPosicion.latitud = loc.getLatitude();
                cPosicion.longitud = loc.getLongitude();
                cPosicion.timeStamp = System.currentTimeMillis();
            }
            if (msg == null) {
                msg = "Invalid broadcast received!";
            }
        } catch (Exception e) {
            Log.e(getClass().getName(), "Exception", e);
        }
    }
}

这就是我启动gps的方式

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        doMySearch(query);
    } else {
        startGPS();
    }
}

// .....
private void startGPS() {
    mgr = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent i = new Intent(this, LocationPoller.class);
    i.putExtra(LocationPoller.EXTRA_INTENT, new Intent(this,
            LocationReceiver.class));
    i.putExtra(LocationPoller.EXTRA_PROVIDER, LocationManager.GPS_PROVIDER);
    pi = PendingIntent.getBroadcast(this, 0, i, 0);
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime(), PERIOD, pi);
}

这就是我关闭活动的方式

private void stopGPS() {
    mgr.cancel(pi);
}

在此先感谢帕梅拉

4

0 回答 0