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);
}
よろしくお願いしますパメラ