ANDROID_MARS学习笔记_S01原始版_013_广播机制二
一、代码
1.xml
(1)main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="绑定监听器"
/>
<Button
android:id="@+id/unregister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="解除监听器绑定"
/> </LinearLayout>
(2)AndroidManifest.xml.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast2"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TestBC2Activity"
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>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>
2.java
(1)TestBC2Activity.java
package com.broadcast2; import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class TestBC2Activity extends Activity {
/** Called when the activity is first created. */
private Button registerButton = null;
private Button unregisterButton = null;
private SMSReceiver smsReceiver = null; private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerButton = (Button)findViewById(R.id.register);
registerButton.setOnClickListener(new RegisterReceiverListener());
unregisterButton = (Button)findViewById(R.id.unregister);
unregisterButton.setOnClickListener(new UnRegisterReceiverListener());
} class RegisterReceiverListener implements OnClickListener{ @Override
public void onClick(View v) {
//生成一个BroiadcastReceiver对象
smsReceiver = new SMSReceiver();
//生成一个IntentFilter对象
IntentFilter filter = new IntentFilter();
//为IntentFilter添加一个Action,决定了reciver能接收什么类型的请求
filter.addAction(SMS_ACTION);
//将BroadcastReceiver对象注册到系统当中
TestBC2Activity.this.registerReceiver(smsReceiver, filter);
} } class UnRegisterReceiverListener implements OnClickListener{ @Override
public void onClick(View v) {
//解除BroadcastReceiver对象的注册
TestBC2Activity.this.unregisterReceiver(smsReceiver);
} }
}
(2)SMSReceiver.java
package com.broadcast2; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage; public class SMSReceiver extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("receive message"); //此例子是获取短信内容
//接受Intent对象当中的数据
Bundle bundle = intent.getExtras();
//在Bundle对象当中有一个属性名为pdus,这个属性的值是一个Object数组
Object[] myOBJpdus = (Object[]) bundle.get("pdus");
//创建一个SmsMessage类型的数组
SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
System.out.println(messages.length);
for (int i = 0; i<myOBJpdus.length; i++)
{
//使用Object数组当中的对象创建SmsMessage对象
messages[i] = SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);
//调用SmsMessage对象的getDisppalyMessageBody()方法,就可以得到消息的内容
System.out.println(messages[i].getDisplayMessageBody());
}
try {
Thread.sleep(30 * 1000);
System.out.println("-------------------------------");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
ANDROID_MARS学习笔记_S01原始版_013_广播机制二的更多相关文章
- ANDROID_MARS学习笔记_S01原始版_012_广播机制一
一.简介 二.代码1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"?> ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词
一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...
- ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast
一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- ANDROID_MARS学习笔记_S01原始版_004_TableLayout
1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...
- ANDROID_MARS学习笔记_S01原始版_003_对话框
1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...
- ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用
一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- ANDROID_MARS学习笔记_S01原始版_001_Intent
一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词
一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3
一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过 ...
随机推荐
- 20160507-hibernate入门
关联映射 多对一(Employee - Department) 一对多(Department-Employee) 一对一(Person - IDCard) 多对多(teacher - student) ...
- java web 优化札记
1.效果最明显最简单最省事的优化是SSD,一般优化效率3倍起,(未必对,但是说明很多瓶颈问题都是存储问题) 2.垂直扩容省了开发时间,短期来看是最快的,缺点是会消耗更多的资源,而且有瓶颈,另外如果应用 ...
- Checkbox 全选、反选
1.全选.反选 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></t ...
- JEditorPane中html文档中文乱码解决
Culturally dependent information in some documents is handled through a mechanism called character e ...
- iOS 9 适配需要注意的问题
iOS 9 适配需要注意的问题 1`网络适配_改用更安全的HTTPS iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL.采用TLS 1.2 协 ...
- java File详解
一.简介 File类是“文件”和“目录名”的抽象表示形式.因此在java语言中,File类既可以表示文件也可以表示目录. 尽管java.io定义的大多数类是实行流式操作的,而File类则不是,它没有指 ...
- hdu 5056 Boring count
贪心算法.需要计算分别以每个字母结尾的且每个字母出现的次数不超过k的字符串,我们设定一个初始位置s,然后用游标i从头到尾遍历字符串,使用map记录期间各个字母出现的次数,如果以s开头i结尾的字符串满足 ...
- android驱动程序之 - sensor
上图是android系统架构图,从中可以得知,sensor必贯穿架构的各个层次.按照架构层次,下面从五个方面来分析sensor架构: 1. sensor架构之App层: 2. sensor架构之Fra ...
- Struts2 Annotation 默认返回Tiles2布局
Struts2的annotation方式很简约,特别实在遵从默认约定的时候就根本不需要配什么struts.xml.网上关于Annotation约定大于配置的教程也很多,其中也不乏将xml版struts ...
- 各种OS间文件传输
搞了几天才会这个法子,羞愧难当. Ubuntu安装iptux,windows下是飞鸽传输.同局域网下可以聊天,传送文件或文件夹.文件夹速度大概10M/S. 其他共享方法: ftp服务器,不成功 sam ...