zoukankan      html  css  js  c++  java
  • Android广播机制的基本使用

    一提到广播我们第一感觉就会联想到小时候村里面的广播,安卓的广播机制也是类似于大喇叭。有发送广播的地方,也有接收广播的地方。但是具体怎么操作呢,我们来一步一步的看下去~

    安卓的广播种类

    如何发送广播

    • 使用意图Intent发送

    1.创建好Intent发送对象,构造方法如下

    //参数是需要发送的信息,一般是字符串
    public static final String ACTION_INTENT_TEST = "com.tao.broadCastReceive";  
     Intent intent = new Intent(ACTION_INTENT_TEST);  
    ```           
    2.使用Activity中的一个方法进行发送广播
    + 同步广播
    同步广播可以几乎再一瞬间到达所有的广播接收站,程序对此无法控制,不能停止,不能阶段
    ```java
    //此方法是Context中的方法,同步广播
    sendBroadcast(intent);  
    
    • 有序广播
      广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,在AndroidManifest.xml的设置;
      比如存在3个广播接收者A、B、C,优先级A>B>C,因此A最先收到广播,当A收到广播后,可以向广播中可以:
    • 添加一些数据给下一个接收者(intent.putExtra())
    • 终止广播(abortBroadcast());
    sendOrderedBroadcast(intent,null);  
    

    广播的接收

    1. 构建一个类,此类继承BroadcastReceiver,并实现onReceive方法,但是记住,onReceive的代码不宜过长。不适合执行长时间操作的代码,如I/O,网络请求,文件读取的等,否则会出现吧ANR(Application No Response),应当创建并启动一个Services并启动,再服务中执行操作(记住服务是工作在UI线程中的哦!!!)
    public class Receiver extends BroadcastReceiver{  
        public void onReceive(Context context, Intent intent) {  
            Bundle bundle = intent.getExtras();  
            ...  
        }  
    } 
    
    1. 再AndroidMainFest.xml文件中注册广播
        <receiver android:name=".Receiver">   
            <intent-filter android:priority="1000">   
                <action android:name="com.tao.broadCastReceive"/>  
            </intent-filter>  
        </receiver> 
    

    这样就完成一个广播的发送了,当然如果需要接收系统的广播,则可以在AndroidMainFest文件中进行设置Action属性,则就可以接收系统发出的广播信息。

    实例代码

    • MainActivity.java
    public class MainActivity extends AppCompatActivity {
        private Button sendBroadCastReceive;
        private EditText titleContent;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sendBroadCastReceive= (Button) findViewById(R.id.sendBroadCastReceive);
            titleContent= (EditText) findViewById(R.id.titleContent);
            sendBroadCastReceive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent sendBroadCast=new Intent();
                    sendBroadCast.setAction("coom.tao.selfBroadCastReceive");
                    sendBroadCast.putExtra("title",titleContent.getText().toString().trim());
                    sendBroadcast(sendBroadCast);
                }
            });
        }
    }
    
    • activity_main.xml 布局文件
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.tao.boedercast.MainActivity">
        <EditText
            android:id="@+id/titleContent"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginBottom="10dp"
            android:layout_above="@+id/sendBroadCastReceive"
            android:hint="请输入发送的内容"
            />
        <Button
            android:id="@+id/sendBroadCastReceive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="发送广播"
            />
    </RelativeLayout>
    

    预览
    image.png

    • 自定义接收BroadcastReceive
    public class MyBroadCastReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String titleContent=intent.getStringExtra("title");
            Toast.makeText(context,titleContent,Toast.LENGTH_LONG).show();
        }
    }
    
    • androidmainfest.xml文件注册
       <receiver android:name=".MyBroadCastReceive">
          <intent-filter android:priority="1000">
              <action android:name="coom.tao.selfBroadCastReceive"></action>
           </intent-filter>
       </receiver>
    

    一个简单的自定义广播完成了,欢迎大家指正批评,谢谢!

    本博客内容一致同步到本人的简书博客:http://www.jianshu.com/p/c01397361ba0 欢迎访问留言交流

  • 相关阅读:
    简单线程池的设计
    MFC开发--截图工具
    Win32小游戏--蜘蛛纸牌
    C++语言动态创建对象
    C++语言类之间的关系
    c++语言虚函数实现多态的原理(更新版)
    5-24 c++语言之【基础知识】
    小细节--关于printf的输出问题
    Win32小游戏--贪吃蛇
    2020北大夏令营 终末游记
  • 原文地址:https://www.cnblogs.com/zhoutao825638/p/10381996.html
Copyright © 2011-2022 走看看