Comments (2)
问题 :华为GEM-703L 网络广播接收问题
详细:在Push进程动态注册的广播:android.net.conn.CONNECTIVITY_CHANGE,关闭断开是可以收到,但是连接时这个广播无法收到,如果在主进程动态注册的话是可以正常收到的。
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void regist(Context context) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
// intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
// intentFilter.addAction(WifiManager.EXTRA_SUPPLICANT_CONNECTED);
// intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
// intentFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
context.registerReceiver(this, intentFilter);
}
经过进一步测试,发现数据流量打开和关闭时是可以正常接收的。
不过,如果监听WiFi的广播:WifiManager.NETWORK_STATE_CHANGED_ACTION,是可以正常接收的。
from androidbox.
考虑用新的接口ConnectivityManager.NetworkCallback来监听网络变化,存在兼容性问题
新增一个NetworkCallBack 完成注册回调监听
public class NetworkCallBack {
private static final String TAG = LogTag.tag("NetworkCallBack");
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void initNetWorkCallBack(Context context, final NetworkReceiver.NetworkListener networkListener) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.registerNetworkCallback(new NetworkRequest.Builder().build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
LogUtil.d(TAG,"onAvailable");
// networkListener.onNetOn();
}
@Override
public void onLosing(Network network, int maxMsToLive) {
LogUtil.d(TAG," onLosing");
}
@Override
public void onLost(Network network) {
LogUtil.d(TAG," onLost");
// networkListener.onNetOff();
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
LogUtil.d(TAG," onCapabilitiesChanged");
}
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
LogUtil.d(TAG,"onLinkPropertiesChanged ");
}
});
}
}
原始注册的地方进行高低版本的兼容
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void regist(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
NetworkCallBack.initNetWorkCallBack(context, networkListener);
LogUtil.d(TAG, " API==" + Build.VERSION.SDK_INT);
return;
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
// intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
// intentFilter.addAction(WifiManager.EXTRA_SUPPLICANT_CONNECTED);
// intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
// intentFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
context.registerReceiver(this, intentFilter);
}
对兼容性进行实验性探索
跑不到的情况
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void regist(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
NetworkCallBack.initNetWorkCallBack(context, networkListener);
LogUtil.d(TAG, " API==" + Build.VERSION.SDK_INT);
// 在低版本机器上跑,这行代码理论上不会跑
new ConnectivityManager.NetworkCallback();
return;
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(this, intentFilter);
}
能跑到的情况
// 代码
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void regist(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
NetworkCallBack.initNetWorkCallBack(context, networkListener);
LogUtil.d(TAG, " API==" + Build.VERSION.SDK_INT);
return;
}
// 在低版本机器上跑,这行代码会跑到
new ConnectivityManager.NetworkCallback();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(this, intentFilter);
}
// 异常:
com.xtc.im.phonesample:push E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.net.ConnectivityManager$NetworkCallback
at com.xtc.im.core.push.receiver.NetworkReceiver.a(Unknown Source)
at com.xtc.im.core.push.PushService.b(Unknown Source)
at com.xtc.im.core.push.PushService.a(Unknown Source)
at com.xtc.im.core.push.PushService.onCreate(Unknown Source)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2723)
at android.app.ActivityThread.access$1600(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1458)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5398)
from androidbox.
Related Issues (20)
- Toast Show的时候出现 BadTokenException: Unable to add window -- token
- java.lang.IllegalStateException: Fragment already added HOT 2
- ANR: "main" prio=5 tid=1 WaitingForGcToComplete HOT 2
- Exception: The specified message queue synchronization barrier token has not been posted or has already been removed.
- ExceptionInInitializerError HOT 2
- 为什么Receiver不允许进行BindService
- Fragment生命周期
- SQLite多线程问题 HOT 3
- 《Android应用性能优化》 HOT 1
- 内存优化 HOT 1
- Native闪退 HOT 1
- java.lang.IllegalStateException: The specified child already has a parent.
- RxJava线程 HOT 1
- Service启动成功onCreate和onStartCmmand都不回调
- CursorWindowAllocationException HOT 1
- SharedPreferences解析
- Component class com.huawei.hms.support.api.push.PushEventReceiver does not exist in com.xtc.watch HOT 2
- SQLite查询操作源码分析:从 IllegalArgumentException:the bind value at index 2 is null 说起
- startActivityForResult的坑
- java.lang.IllegalArgumentException: pointerIndex out of range HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from androidbox.