Android 为移动数据状态更改侦听器设置广播接收器

Android 为移动数据状态更改侦听器设置广播接收器,android,broadcastreceiver,Android,Broadcastreceiver,我想为任何类型的移动数据设置接收器,如3g等 我在接收时使用此代码 但在某些设备中,它不能正常工作,许多情况下,接收器会在一段时间后触发,而我不会触发BroadcastReceive的onReceive() 请帮助我,要么我通过错误的代码,要么它需要一些其他技巧来解决 ConnectivityManager conMan = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkI

我想为任何类型的移动数据设置接收器,如3g等

我在接收时使用此代码

但在某些设备中,它不能正常工作,许多情况下,接收器会在一段时间后触发,而我不会触发BroadcastReceive的onReceive()

请帮助我,要么我通过错误的代码,要么它需要一些其他技巧来解决

ConnectivityManager conMan = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo netInfo = conMan.getActiveNetworkInfo();

if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
    Toast.makeText(context, "Mobile data enable", Toast.LENGTH_LONG).show();
}
这是舱单上的库德

  <receiver android:name=".BroadcastReceiverforData" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
        </intent-filter>
    </receiver>


提前感谢…

这可能会对您有所帮助。在广播接收器中添加此选项

if(intent.getAction()=="android.net.wifi.STATE_CHANGE" || intent.getAction()=="android.net.conn.CONNECTIVITY_CHANGE"){
NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
NetworkInfo netInfo1 = intent.getParcelableExtra(ConnectivityManager.CONNECTIVITY_ACTION);
        if( netInfo.isConnected() ){
        ///do
        }
        if( netInfo1.isConnected() ){
        ///do
        }