广播

来源:互联网 发布:淘宝卖刀具犯法吗 编辑:程序博客网 时间:2024/05/21 07:15
BroadCastReceiver工作过程


广播的使用方法:
1.定义广播接收者(继承BroadCastRecevier)
广播的注册
 静态注册
   <receiver android:name=".MyReceiver">  
            <intent-filter>  
                <action android:name="android.intent.action.MY_BROADCAST"/>  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
    </receiver>  
 动态注册
   MyReceiver receiver = new MyReceiver();  
   IntentFilter filter = new IntentFilter();  
   filter.addAction("android.intent.action.MY_BROADCAST");  
   registerReceiver(receiver, filter);  
广播的发送与接收
    public void send(View view) {  
         Intent intent = new Intent("android.intent.action.MY_BROADCAST");  
         intent.putExtra("msg", "hello receiver.");  
         sendBroadcast(intent);  
    }  

2. 有序广播与普通广播
  普通广播(Normal Broadcast)
  普通广播对于多个接收者来说是完全异步的,通常每个接收者都无需等待即可以接收到广播,接收者相互之间不会有影响。
  对于这种广播,接收者无法终止广播,即无法阻止其他接收者的接收动作。
  
  有序广播(Ordered Broadcast)
  有序广播比较特殊,它每次只发送到优先级较高的接收者那里,然后由优先级高的接受者再传播到优先级低的接收者那里,优先级高的接收者有能力终止这个广播。
  
  

3.电量变化、网络状态变化等使用广播





ContentProvider
ActivityThread先加载contentprovider,再调用Application的oncreate方法
UriMather
getType