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时的区别,希望大神能够稍微讲解下,也请回答下上面的第二个问题,
The difference is that it is valid once and valid continuously. The null method only obtains information when it is called at that time. Subsequent changes will not be received because you have no receiver;
First discuss your scenario requirements. In theory, calling queries frequently will not have any impact. It’s just how frequent you are and whether it has practical significance.