android之四大组件之一-BroadcastReceiver的实践使用(二)

一,广播接收器的静态注册

 <!-- 静态注册 -->
        <receiver android:name=".FirstReceiver">
            <intent-filter android:priority="1000">
                <action android:name="com.xu.first"/>
            </intent-filter>
        </receiver>


二,广播接收器的动态注册

//动态注册广播接收器
SecondReceiver receiver=new SecondReceiver();
IntentFilter filter=new IntentFilter(ACTION);
//设置优先级
filter.setPriority(10);
registerReceiver(receiver, filter);


布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播" 
        android:onClick="mySend"/>

</RelativeLayout>

MainActivity:

private static final String ACTION="com.xu.first";
	public void mySend(View view) {
		// TODO Auto-generated method stub
		Intent intent=new Intent();
		intent.setAction(ACTION);
		//发送普通广播
               sendBroadcast(intent);
       
	}



你可能感兴趣的:(四大组件)