22 广播静态创建代码案例

结构:
这里写图片描述

清单文件AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qf.day22_broadcastreceiver_demo1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.qf.day22_broadcastreceiver_demo1.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 
        注册广播接收者
        name="包名.类名"
        设定频道(action)  唯一  
         -->
        <receiver android:name="com.qf.day22_broadcastreceiver_demo1.MyBroadCastReceiver01">
            <intent-filter
                android:priority="20" >
                <action android:name="com.qq.weixin"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.qf.day22_broadcastreceiver_demo1.MyBroadCastReceiver02">
            <intent-filter 
                android:priority="10">
                <action android:name="com.qq.weixin"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.qf.day22_broadcastreceiver_demo1.MyBroadCastReceiver03">
            <intent-filter 
                android:priority="30" >
                <action android:name="com.qq.weixin"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

MainActivity.java

package com.qf.day22_broadcastreceiver_demo1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

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

    //发送广播
    public void MySendClick(View v){

//      Intent intent = new Intent(MainActivity.this, MyBroadCastReceiver01.class);
        Intent intent = new Intent();
        intent.setAction("com.qq.weixin");
        intent.putExtra("str", "疯狗咬人");
        //发送普通的广播
        sendBroadcast(intent);

    }



}

MyBroadCastReceiver01.java

package com.qf.day22_broadcastreceiver_demo1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadCastReceiver01 extends BroadcastReceiver{

    /**
     * 就一个生命周期      普通广播10s
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

    }

}

MyBroadCastReceiver02.java
MyBroadCastReceiver03.java
与上一个代码一样

结果:三个广播接受者 根据priority大小先后弹吐司

posted on 2016-09-21 19:25  木鱼哥  阅读(187)  评论(0编辑  收藏  举报

导航