一.什么是广播?

在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。我们拿广播电台来做个比方。我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内容,而我们用户只需要将频率调成和广播电台的一样就可以收听他们的内容了。Android中的广播机制就和这个差不多的道理。
电台发送的内容是语音,而在Android中我们要发送的广播内容是一个Intent。这个Intent中可以携带我们要传送的数据。
电台通过大功率的发射器发送内容,而在Android中则是通过sendBroadcast这个方法来发送(很形象的名字吧)。
用户通过调整到具体的电台频率接受电台的内容。而在Android中要接受广播中的内容则是通过注册一个BroadCastReceiver来接收的。只有发送广播的action和接收广播的action相同,接受者才能接受这个广播。

二.静态注册广播示例:
在你需要传入的Activity的AndroidMainifest中加入:

 <receiver android:name=".Myresive">          <intent-filter >              <action android:name="hello"/>          intent-filter>      receiver>

在你要传出广播的Activity中代码如下:

package com.example.administrator.myapplicat326;import android.content.Context;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    Button button;    private Context context;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = findViewById(R.id.qw);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Intent intent = new Intent("hello");                sendBroadcast(intent);//静态注册无序列表//                sendOrderedBroadcast(intent, null);            }        });    }}

三.动态注册无序列表:
动态注册就是把原来静态注册写在AndroidMainifest中的以代码形式写出来:

package com.example.administrator.myapplica3266;import android.content.Intent;import android.content.IntentFilter;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {private  Myresive  myresive;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         myresive= new Myresive();        IntentFilter filter = new IntentFilter();        filter.addAction("hello");       registerReceiver(myresive, filter);    }    @Override    protected void onDestroy() {        super.onDestroy();        unregisterReceiver(myresive);    }}

四,静态注册有序列表:
发送广播的不变,Activity的AndroidMainifest中加入:

<receiver android:name=".Myresive">          <intent-filter android:priority="1000" >//优先级              <action android:name="hello"/>          intent-filter>      receiver>

在新建一个类继承BroadcastReceiver :

public class Myresive extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        Toast.makeText(context, "叫我干嘛", Toast.LENGTH_SHORT).show();        Log.e("1111111111111111","叫我干嘛");    }}

五。动态注册有序广播:

动态注册就是把原来静态注册写在AndroidMainifest中的以代码形式写出来:

package com.example.administrator.myapplica3266;import android.content.Intent;import android.content.IntentFilter;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {private  Myresive  myresive;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         myresive= new Myresive();        IntentFilter filter = new IntentFilter();        filter.setPriority(1000);        filter.addAction("hello");       registerReceiver(myresive, filter);    }    @Override    protected void onDestroy() {        super.onDestroy();        unregisterReceiver(myresive);    }}

更多相关文章

  1. Android中我为什么发不了邮件--Android邮件发送详解
  2. Android、pc文件无线双向传输软件
  3. Android实现自定义广播
  4. Android引入广播机制的用意。单线程模型Message、Handler、Messa
  5. Android学习之Android广播机制
  6. 说说android下TV版本UC浏览器模拟鼠标的实现
  7. Android学习之Android广播机制
  8. Android(安卓)BroadcastReceiver(广播)实现消息发送
  9. Android学习札记52:广播(Broadcast)基础篇

随机推荐

  1. android的各种*.img 文件
  2. 1.8 奇葩,android onBackPressed结束了两
  3. android 异常问题解决
  4. 可折叠的列表ExpandableListView及其适配
  5. Android 的AT命令协议栈初始化
  6. android adb网络连接方法
  7. 刚开始安卓,记录一个刚做的图片缩放程序
  8. android 发送http请求
  9. Android Root方法原理解析及Hook(一) adb
  10. 覆盖Android开发各个领域的近百个源码项