本文介绍了设置广播接收机的移动数据状态变化监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要设置接收器启用/禁用如3G等任何类型的移动数据。
I want to set receiver for any type of mobile data enabled/disabled such as 3g etc.
我用这code对接收...
I am using this code for on receive...
但在某些设备无法一段时间后正常工作,许多情况下,接收器触发和一段时间我不会触发广播接收器的的onReceive()?
but in some device it can't work properly and many case receiver trigger after some time and some time i will not trigger onReceive() of Broadcastreceiver ?
请帮我任我经历错了code,或者需要一些其他的把戏来解决?
Please help me either I going through wrong code or it need some other trick to resolve?
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();
}
和本coode在清单
<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>
在此先感谢...
Thanks in advance...
推荐答案
也许这可以帮助你。在广阔的投接收器添加此
Probably this may help you. Add this in broad cast 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
}
这篇关于设置广播接收机的移动数据状态变化监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!