Intent intent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
查询电量的时候可以通过上面这种方式返回一个intent,从这个intent中也确实能够拿到我们想要要的信息,但是平常我们注册普通的广播的时候都习惯传入一个receiver,如果有电量改变就会不停的执行receiver的onReceive方法,我的疑问就是1.这两种注册方法有什么不同之处吗?2.如果我频繁的使用传入空receiver的方法进行电量查询会有什么麻烦吗?
------------------分割线-------------------------------
我也去稍微看了下registerReceiver()方法的源码,应该是在ContextImpl类中的
private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
IntentFilter filter, String broadcastPermission,
Handler scheduler, Context context) {
IIntentReceiver rd = null;
if (receiver != null) {
if (mPackageInfo != null && context != null) {
if (scheduler == null) {
scheduler = mMainThread.getHandler();
}
rd = mPackageInfo.getReceiverDispatcher(
receiver, context, scheduler,
mMainThread.getInstrumentation(), true);
} else {
if (scheduler == null) {
scheduler = mMainThread.getHandler();
}
rd = new LoadedApk.ReceiverDispatcher(
receiver, context, scheduler, null, true).getIIntentReceiver();
}
}
try {
return ActivityManagerNative.getDefault().registerReceiver(
mMainThread.getApplicationThread(), mBasePackageName,
rd, filter, broadcastPermission, userId);
} catch (RemoteException e) {
return null;
}
}
但是作为一个菜鸟还是没能理解这个方法中receiver为null时的区别,希望大神能够稍微讲解下,也请回答下上面的第二个问题,
PHP中文网2017-04-18 09:15:16
차이점은 한 번만 유효하고 지속적으로 유효하다는 점입니다. 해당 시점에 호출될 때만 정보를 얻습니다.