嗨,我正在使用广播接收器进行位置侦听,但是此广播接收器仅在活动处于前台时才起作用,否则当我不使用此应用程序时,接收器不活动这里是活动代码 onCreate 的方法,我在其中注册广播接收器
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
locationManager.addProximityAlert(
latitude, // the latitude of the central point of the alert region
longitude, // the longitude of the central point of the alert region
1000, // the radius of the central point of the alert region, in meters
-1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT+s);
registerReceiver(new ProximityAlertReceiver(), filter);
BroadcastReceiver 的代码是
public class ProximityAlertReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
SharedPreferences shared=context.getSharedPreferences(MainActivity.PREFS_NAME,0);
Boolean entering = intent.getBooleanExtra(key, false);
Double longitude=intent.getDoubleExtra("longitude", 0.0);
Double latitude=intent.getDoubleExtra("latitude",0.0);
Intent in = new Intent(context,AlarmActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("longitude", longitude);
in.putExtra("latitude", latitude);
Log.i("cont1", shared.getInt("count", 0)+"");
in.putExtra("count",shared.getInt("count", 0));
context.startActivity(in);
}
}
我读了一些分别在 onResume 和 onPause 方法中注册和取消注册它的地方,但是当我在 onPause 方法中取消注册它时,它被取消注册意味着它不会工作我希望我的广播接收器一直工作。你能建议如何做到这一点。