Giter VIP home page Giter VIP logo

Comments (2)

zengjingfang avatar zengjingfang commented on September 22, 2024

问题 :华为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.

zengjingfang avatar zengjingfang commented on September 22, 2024

考虑用新的接口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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.