检查意图互联网连接有安卓系统吗?Intent ACTION_XXX当互联网连接可用时通知我?我想实例化一个BroadcastReceiver当用户启用Internet连接(通过wifi、GSM等)时,通知我的应用程序。有人能帮我吗?
3 回答
万千封印
TA贡献1891条经验 获得超3个赞
<receiver android:name=".YOURRECEIVER"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter></receiver><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
隔江千里
TA贡献1906条经验 获得超10个赞
public class NetworkStateReceiver extends BroadcastReceiver { private static final String TAG = "NetworkStateReceiver"; @Override public void onReceive(final Context context, final Intent intent) { Log.d(TAG, "Network connectivity change"); if (intent.getExtras() != null) { final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); if (ni != null && ni.isConnectedOrConnecting()) { Log.i(TAG, "Network " + ni.getTypeName() + " connected"); } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { Log.d(TAG, "There's no network connectivity"); } } }}
- 3 回答
- 0 关注
- 354 浏览
添加回答
举报
0/150
提交
取消