Android(java)学习笔记116:BroadcastReceiver之 静态注册 和 动态注册

时间:2023-03-08 17:04:07

1. 广播接受者
>什么是广播。收音机。
电台:对外发送信号。
收音机:接收电台的信号。

>在android系统里面,系统有很多重要的事件: 电池电量低,插入充电器,sd卡被移除,有电话打出去,有短信发送进来。

静态注册,使用广播机制步骤:
(1)买收音机
        public class SDStatusReceiver extends BroadcastReceiver
(2)装电池
         <receiver android:name="com.itheima.sdstatus.SDStatusReceiver" >
(3)调频道,调到你关心的频道
         <intent-filter >
                <!-- 3.调频道 -->
                <action android:name="android.intent.action.MEDIA_MOUNTED"/>
                <data android:scheme="file"/>
         </intent-filter>

动态注册,使用广播机制步骤:

(1)在Activity中的onCreate 或者 onResume中注册广播接受者,如下:

public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter):Activity的方法

(2)在Activity中的onDestory方法中注销广播接受者,如下:

public void unregisterReceiver(BroadcastReceiver receiver):Activity的方法

2. 静态注册和动态注册区别:

(1)动态注册的广播 永远要快于 静态注册的广播,不管静态注册的优先级设置的多高,不管动态注册的优先级有多低

(2)动态注册广播不是 常驻型广播 ,也就是说广播跟随activity的生命周期。注意: 在activity结束前,移除广播接收器。

    静态注册是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行

(3)在同一个优先级下谁先启动的快谁将先接收到广播

3. 下面查看这个广播接收者静态注册案例:

   当应用程序关闭了,如果有广播信息来,写的广播接收器同样的能接收到,它的注册方式就是在应用程序的AndroidManifast.xml 中进行注册,这种注册方式通常又被称作静态注册。这种方式可以理解为通过清单文件注册的广播是交给操作系统去处理的。

下面是工程:

Android(java)学习笔记116:BroadcastReceiver之 静态注册 和 动态注册

这里我们MainActivity.java和activity_main.xml文件,我们不做什么修改。

主要还是按照上面的使用广播机制步骤:

(1)买收音机,自定义一个SDStatusReceiver类,它继承自BroadcastReceiver(BroadcastReceiver是提供的基类API接口)

onReceive方法是接收到广播执行的操作内容。

package com.itheima.sdstatus;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; /**
* 1.买好了一个收音机
*/
public class SDStatusReceiver extends BroadcastReceiver { // 用来接收广播事件的,一旦广播消息到来,就会执行onreceive方法
@Override
public void onReceive(Context context, Intent intent) {
//public String getAction():返回一个action的名称
String action = intent.getAction();
if ("android.intent.action.MEDIA_UNMOUNTED".equals(action)) {
System.out.println("哈哈哈----SD卡被卸载了。");
Toast.makeText(context, "哈哈哈----SD卡被卸载了,微信头像暂不可用", 0).show();
}else if("android.intent.action.MEDIA_MOUNTED".equals(action)){
Toast.makeText(context, "哈哈哈----SD卡被挂载了", 0).show();
}
} }

(2)(3)装电池、调频道,在AndroidManifest.xml清单文件中进行配置。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.sdstatus"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.sdstatus.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>
<!-- 2.装电池 -->
<receiver android:name="com.itheima.sdstatus.SDStatusReceiver" >
<intent-filter >
<!-- 3.调频道,这里我们可以设置多个action,监听多个广播频道 -->
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file"/>
</intent-filter>
</receiver> </application> </manifest>

注意:就算是我们的应用程序没有启动运行,只要SD卡卸载或者安装,就会促使应用程序启动线程运行,这样的话运行onReceive()里面的代码。这里采用是注册事件的机制,一旦系统检查到SD卡卸载或者安装,Android系统会主动注册监听(SD卡卸载或者安装)的应用程序。

4.  BroadcastReceiver动态注册注销:

(1)工程一览图:

Android(java)学习笔记116:BroadcastReceiver之 静态注册 和 动态注册

(2)activity_main.xml :

 <LinearLayout 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"
android:orientation="vertical"
tools:context="com.himi.BroadcastReceiverDemo.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/btnSendMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送消息" /> <Button
android:id="@+id/btnReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册接收器" /> <Button
android:id="@+id/btnUnreg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注销接收器" /> </LinearLayout>

布局效果,如下:

Android(java)学习笔记116:BroadcastReceiver之 静态注册 和 动态注册

(3)AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.himi.BroadcastReceiverDemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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> </application> </manifest>

(4)MyReceiver.java:

 package com.himi.BroadcastReceiverDemo;

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class MyReceiver extends BroadcastReceiver {
public static final String ACTION = "com.himi.BroadcastReceiverDemo.intent.action.MyReceiver"; public MyReceiver() { } @Override
public void onReceive(Context context, Intent intent) {
System.out.println("接收到消息了,消息的内容是:" + intent.getStringExtra("data")); } }

(5)MainActivity.java:

 package com.himi.BroadcastReceiverDemo;

 import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSendMsg).setOnClickListener(this);
findViewById(R.id.btnReg).setOnClickListener(this);
findViewById(R.id.btnUnreg).setOnClickListener(this);
} public void onClick(View v) {
switch(v.getId()) {
case R.id.btnSendMsg:
//Intent i= new Intent(this, MyReceiver.class);
Intent i = new Intent(MyReceiver.ACTION);
i.putExtra("data", "jikexueyuan");
sendBroadcast(i);
break; case R.id.btnReg:
if(receiver == null) {
receiver = new MyReceiver();
registerReceiver(receiver, new IntentFilter(MyReceiver.ACTION));
}
break; case R.id.btnUnreg:
if(receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
break; } } private MyReceiver receiver = null; }