您的位置:首页 > 移动开发 > Android开发

Android(java)学习笔记173:BroadcastReceiver之 BroadcastReceiver静态注册(SD卡卸载或者安装案例)和 BroadcastReceiver动态注册注销

2015-08-18 16:47 696 查看
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>



2. 广播事件的两种类型。


(1) 有序广播


> 接受者有优先级,接受按照先后顺序接受,类似中央向下传文件。高优先级的接受者可以把广播消息给拦截,还可以修改广播的数据。


(2)无序广播


> 接受者没有优先级,没有先后顺序。类似听广播,看新闻联播。不可以被拦截。


> sendBroadcast(intent)



3. 有序广播和无序广播没有什么本质的区别,


相同点:

无序广播也是按照先后顺序接受的

不同点:

无序广播如果拦截,会拦截失败。

不可以修改数据。


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

下面是工程:





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

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

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

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

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()里面的代码。这里采用是注册事件的机制,一旦系统检查到[b]SD卡卸载或者安装,Android系统会主动注册监听([b][b]SD卡卸载或者安装)的应用程序。[/b][/b][/b]

5. BroadcastReceiver动态注册注销:

(1)工程一览图:





(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>


布局效果,如下:





(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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: