我想在broadcastReceiver中检查设备是否已连接。 下面是我的代码:
public boolean isOnline(Context context) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
Log.e("UpdateDataReceiver","info: "+info);
return false;
}
return true;
}
我的代码问题: 当BroadcastReceiver在后台触发时(当app在后台时),上面的函数会返回false(即使连接了wifi) 并且当app在前台时它返回true。
info:NetworkInfo:type:WIFI [],state:DISCONNECTED / BLOCKED,原因: (未指定),额外:(无),漫游:false,故障转移:false, isAvailable:true,isConnectedToProvisioningNetwork:false,simId:0
设备信息:Redmi Note
答案 0 :(得分:4)
这就是我处理它的方式,因为即使有网络连接,getActiveNetworkInfo
也会在特定情况下始终返回DISCONNECTED / BLOCKED。这是BroadcastReceiver
中具有意图filter ConnectivityManager.CONNECTIVITY_ACTION
的接收方法。
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conn.getActiveNetworkInfo();
NetworkInfo intentNetworkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (intentNetworkInfo == null) {
intentNetworkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
}
if (networkInfo == null) {
networkInfo = intentNetworkInfo;
} else {
//when low battery get ActiveNetwork might receive DISCONNECTED/BLOCKED but the intent data is actually CONNECTED/CONNECTED
if (intentNetworkInfo != null && networkInfo.isConnectedOrConnecting() != intentNetworkInfo.isConnectedOrConnecting()) {
networkInfo = intentNetworkInfo;
}
}
//do something with networkInfo object
}
我已经搜索了更好的解决方案,但没有结果。我能够在我的设备(Pixel 7.1.2)上重现100%的情况如下:
getActiveNetworkInfo
的DISCONNECTED / BLOCKED。 如果您在应用程序中更改连接,它将是正常的,但如果它在背景上它不会。在调试时不会发生这种情况,因为即使电池电量不足也会给设备充电。
在上面的示例中,ConnectivityManager和WifiManager中的EXTRA_NETWORK_INFO实际上是相同的字符串" networkInfo"但是如果在其他Android版本中它们不同,我也不会冒险,所以它是额外的样板。
您可以直接使用意图中的networkInfo,但我想在此处显示有一种情况,即actualNetworkInfo不是实际的网络信息。
答案 1 :(得分:1)
我相信你能做到这一点的方式是,
使用ConnentManger.Connectivity_Action的IntentFilter注册广播接收器
WM_USBRDRESPONSE
然后在你的广播接收器类
private BroadcastReceiver receiverDataChange;
private void registerData(){
try {
receiverDataChange = new bcr_ToggleData();
IntentFilter filterData = new IntentFilter();
filterData.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(receiverDataChange, filterData);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}}
}
当您进入设置并打开/关闭移动数据时,会启动此功能。
希望这有帮助! :)
答案 2 :(得分:0)
extension UIFont
{
static func fractionFont(ofSize pointSize: CGFloat) -> UIFont
{
let systemFontDesc = UIFont.systemFont(ofSize: pointSize).fontDescriptor
let fractionFontDesc = systemFontDesc.addingAttributes(
[
UIFontDescriptorFeatureSettingsAttribute: [
[
UIFontFeatureTypeIdentifierKey: kFractionsType,
UIFontFeatureSelectorIdentifierKey: kDiagonalFractionsSelector,
], ]
] )
return UIFont(descriptor: fractionFontDesc, size:pointSize)
}
}
let label = UILabel()
label.backgroundColor = .white
let pointSize: CGFloat = 45.0
let string = "This is a mixed fraction 312/13"
let attribString = NSMutableAttributedString(string: string, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: pointSize), NSForegroundColorAttributeName: UIColor.black])
attribString.addAttributes([NSFontAttributeName: UIFont.fractionFont(ofSize: pointSize)], range: (string as NSString).range(of: "12/13"))
label.attributedText = attribString
label.sizeToFit()
我在这个谷歌教程中找到了这个:http://developer.android.com/intl/pt-br/training/monitoring-device-state/connectivity-monitoring.html。看看吧。