由网友(结局是灰色的)分享简介:我要检查,当手机在Android的网络熄灭。我可以捕捉事件?我没有得到适当的API或任何例子可以解释相同。如果有人做了或者例如链接将是非常有益的。先谢谢了。I want to check when the network of phone in Android goes off. Can I capture th...
我要检查,当手机在Android的网络熄灭。我可以捕捉事件? 我没有得到适当的API或任何例子可以解释相同。如果有人做了或者例如链接将是非常有益的。 先谢谢了。
I want to check when the network of phone in Android goes off. Can I capture that event? I am not getting the proper API or any example which would explain the same. If anyone had done or any example links would be really helpful. Thanks in advance.
推荐答案
新的Java类:
public class ConnectionChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent )
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
if( mobNetInfo != null )
{
Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
}
}
在你的AndroidManifest.xml中新的XML下的清单的元素:
New xml in your AndroidManifest.xml under the "manifest" element:
<!-- Needed to check when the network connection changes -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
在你的AndroidManifest.xml中新的XML下的应用程序的元素:
New xml in your AndroidManifest.xml under the "application" element:
<receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
相关推荐
最新文章