Android Studio 学习(三) 广播

动态注册监听网络变化

  • 创建intentFilter 并addAction 代表了监听哪个广播
  • 然后使用registerReceiver()方法 将intentFilter 与 自己创建的监听器 传进去
  • 动态注册的 需要unregisterReciver()
  • 权限需要在Mainfest中声明 <uses-permission android :name "android.permission.ACCESS_NETWORK.STATE"/>

public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangedReceiver networkChangedReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangedReceiver = new NetworkChangedReceiver();
registerReceiver(networkChangedReceiver,intentFilter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(networkChangedReceiver);
}
class NetworkChangedReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive (Context context,Intent intent)
    {
        ConnectivityManager connectivityManager =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null && networkInfo.isAvailable())
        {
            Toast.makeText(context,"good",Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(context,"bad",Toast.LENGTH_LONG).show();
        }

    }
}

}

静态注册实现开机启动

  • new-> other->broadcast receiver 手动创建广播
  • exported 允许此广播接收本程序外的广播

之后再Mainfest中声明权限
并说明要监听的广播

发送广播

  • Intent intent = new Intent("com....")
  • sendBroadcast(intent);
  • sendOrderedBroadcast(intent,null); 发送有序广播

  • <intent- filter android :priority="100">//设置广播优先级
  • abortBroadcast() 截断广播

本地广播

public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //得到本地广播管理器的实例
    localBroadcastManager = LocalBroadcastManager.getInstance(this);

    //动态注册本地广播接收器
    intentFilter = new IntentFilter();
    intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
    localReceiver = new LocalReceiver();
    localBroadcastManager.registerReceiver(localReceiver, intentFilter);

    //发送本地广播
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
            localBroadcastManager.sendBroadcast(intent);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();

    //取消动态网络变化广播接收器的注册
    localBroadcastManager.unregisterReceiver(localReceiver);
}


class LocalReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
    }
}

}

Android Studio 学习(三) 广播

上一篇:leetcode 72. 编辑距离


下一篇:Jsp内置对象application之统计浏览网页的次数