zoukankan      html  css  js  c++  java
  • 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大小先后弹吐司

  • 相关阅读:
    kibana x-pack 更新license
    elastic search集群请求超时
    angular4 angular/cli 版本升级
    windows 编写shell 脚本转化成unix
    spring boot 自定义HandlerMethodArgumentResolver做参数校验
    Fiddler 重定向BUG
    scoop
    acm每日刷题记录
    ccpc 2016 changchun 长春(4.12训练)
    cf 338E
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152198.html
Copyright © 2011-2022 走看看