我有一个活动,有两个广播接收器,以获得电池寿命和GPS位置。我根据开发者指南在onResume()中注册了它们并注销了onPause()。但接收器中的onReceive()方法被无限调用。这是我的代码:
public class BatteryStatusReceiver extends BroadcastReceiver {
private final static String TAG = "BatteryStatusReceiver";
private Activity mActivity = null;
public BatteryStatusReceiver(Activity activity){
this.mActivity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Log.isLoggable(TAG, Log.DEBUG))
Log.d(TAG, "action:" + action);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
//This will calll mActivity.onNewIntent method.
if (mActivity!=null){
Intent i = new Intent(context, mActivity.getClass());
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra(BatteryManager.EXTRA_LEVEL, level);
i.putExtra(BatteryManager.EXTRA_SCALE, scale);
context.startActivity(i);
}
}
}
public class LocationReceiver extends BroadcastReceiver {
private Activity mActivity;
public LocationReceiver(Activity activity) {
this.mActivity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(LocationService.ACTION_GPS_ACTIVATED)){
double[] location = intent.getExtras().getDoubleArray(ConstDef.KEY_LOCATION); //lat, lng
//This will calll mActivity.onNewIntent method.
if (mActivity!=null){
Intent i = new Intent(context, mActivity.getClass());
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra(ConstDef.KEY_LOCATION, location);
context.startActivity(i);
}
}
}
}
//LocationService
public class LocationService extends Service implements LocationListener {
/* (non-Javadoc)
* @see android.app.Service#onStartCommand(android.content.Intent, int, int)
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (action.equals(ACTION_START))
processStart(ONE_MINUTE*10, 100);
else if (action.equals(ACTION_STOP))
processStop();
return START_NOT_STICKY;
}
/* (non-Javadoc)
* @see android.location.LocationListener#onLocationChanged(android.location.Location)
*/
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged");
if (isBetterLocaion(location, mCurrentBestLocation))
mCurrentBestLocation = location;
broadcastLocation(mCurrentBestLocation);
}
/**
* Send location to the receivers
* @param location
*/
protected void broadcastLocation(Location location){
Intent intent = new Intent(ACTION_GPS_ACTIVATED);
intent.putExtra(ConstDef.KEY_LOCATION, new double[]{location.getLatitude(), location.getLongitude()});
sendBroadcast(intent);
}
}
//Activity
private BatteryStatusReceiver mBatteryReceiver = new BatteryStatusReceiver(this);
@Override
protected void onResume() {
super.onResume();
//Activate location service to get the response from GPS as soon as possible before calling.
startService(new Intent(LocationService.ACTION_START));
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBatteryReceiver, ifilter);
}
@Override
protected void onNewIntent(Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryFull = level / (float)scale;
//if (Log.isLoggable(TAG, Log.DEBUG))
Log.d(TAG, "Level:" + level + " Scale:" + scale + ", batteryFull:" + batteryFull);
String dateTime = JDateUtils.now(JDateUtils.LONG_DATE_FORMAT);
//TODO. Process here...
}
@Override
protected void onPause() {
super.onPause();
stopService(new Intent(this, LocationService.class));
unregisterReceiver(mBatteryReceiver);
}
}
我的代码中缺少什么?请帮我解决我的问题。提前谢谢。