Android学习之Broadcast的简单使用-创新互联
本文实例为大家分享了Android学习之Broadcast的使用方法,供大家参考,具体内容如下
创新互联专注于企业成都营销网站建设、网站重做改版、海城网站定制设计、自适应品牌网站建设、H5场景定制、成都商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为海城等各大城市提供网站开发制作服务。实现开机启动提示网络的广播
package com.example.luobo.broadcasttest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intentFilter = new IntentFilter();//创建一个过滤器实例 intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//添加接收CONNECTIVITY_CHANGE消息 networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver,intentFilter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(networkChangeReceiver); } class NetworkChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);//通过此方法获取ConnectivityManager实例 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();//调用实例connectivityManager的getActiveNetworkInfo()方法获取NetworkInfo实例 if (networkInfo != null && networkInfo.isAvailable()){ Toast.makeText(context,"Network is available",Toast.LENGTH_SHORT).show(); }else { Toast.makeText(context,"Network is unavailable",Toast.LENGTH_SHORT).show(); } } } }
网页标题:Android学习之Broadcast的简单使用-创新互联
网站链接:http://pwwzsj.com/article/ccdhco.html