-----------------AndroidManifest.xml-------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.homer.receiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<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>
<receiver android:name="MyReceiver">
<intent-filter>
<!-- android.intent.action.STATIC_BROADCAST为自定义的静态广播标记
当MainActivtiy中发出android.intent.action.STATIC_BROADCAST Intent时
MyRecevicer会起相应执行MyRecevicer类的onReceive方法
-->
<action android:name="android.intent.action.STATIC_BROADCAST" />
<!-- android.provider.Telephony.SMS_RECEIVED为android系统定义的短信广播标记
当手机收到短信时 MyRecevicer会起相应 执行MyRecevicer类的onReceive方法
-->
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<span style="color: rgb(77, 109, 243); font-family: monospace; line-height: 18px; white-space: nowrap;"><!-- </span><wbr style="color: rgb(77, 109, 243); font-family: monospace; line-height: 18px; white-space: nowrap;"><span style="color: rgb(77, 109, 243); font-family: monospace; line-height: 18px; white-space: nowrap;">添加程序接收短信的权限 </span><wbr style="color: rgb(77, 109, 243); font-family: monospace; line-height: 18px; white-space: nowrap;"><span style="color: rgb(77, 109, 243); font-family: monospace; line-height: 18px; white-space: nowrap;">--></span>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest></wbr></wbr>
--------------------MainActivity.java-------------
package com.homer.receiver;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.homer.receiver.MyReceiver;
public class MainActivity extends Activity implements OnClickListener{
private static final String TAG="MainActivity";
private Button sendstatic;
private Button senddynamic;
private MyReceiver myreceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myreceiver=new MyReceiver();
findViews();//获取句柄
setListener();//实现行为功能
}
//Activity创建或者从后台重新回到前台时被调用
@Override
protected void onStart() {
Log.v(TAG, "onStart()");
super.onStart();
IntentFilter dynamic_filter = new IntentFilter();
dynamic_filter.addAction("android.intent.action.DYNAMIC_BROADCAST");
<!-- android.intent.action.DYNAMIC_BROADCAST为自定义的动态广播标记
当MainActivtiy中发出android.intent.action.DYNAMIC_BROADCAST Intent时
myrecevicer会起相应执行myrecevicer类的onReceive方法
-->
registerReceiver(myreceiver, dynamic_filter);
}
//Activity从后台重新回到前台时被调用
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart called.");
}
//Activity创建或者从被覆盖、后台重新回到前台时被调用
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume called.");
}
private void findViews(){
sendstatic=(Button)findViewById(R.id.send_static);
senddynamic=(Button)findViewById(R.id.send_dynamic);
}
private void setListener(){
sendstatic.setOnClickListener(this);
senddynamic.setOnClickListener(this);
}
public void onClick(View v) {
Log.v(TAG, "onClick()");
switch(v.getId()){
case R.id.send_static:
Intent intent = new Intent("android.intent.action.STATIC_BROADCAST");
intent.putExtra("msg", "接受静态注册广播成功");
sendBroadcast(intent);
break;
case R.id.send_dynamic:
Intent intent1 = new Intent("android.intent.action.DYNAMIC_BROADCAST");
intent1.putExtra("msg", "接受动态注册广播成功");
sendBroadcast(intent1);
break;
}
}
//Activity被覆盖到下面或者锁屏时被调用
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause called.");
//有可能在执行完onPause或onStop后,系统资源紧张将Activity杀死,所以有必要在此保存持久数据
}
//退出当前Activity或者跳转到新Activity时被调用
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop called.");
}
@Override
protected void onDestroy() {
Log.v(TAG, "onDestroy()");
super.onDestroy();
unregisterReceiver(myreceiver);
}
}
---------------------MyRecevicer.java----------------
<span style="font-size:18px;">package com.homer.receiver;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceiver");
if(intent.getAction().equals("android.intent.action.DYNAMIC_BROADCAST")){
String msg = intent.getStringExtra("msg");
Log.i(TAG, msg);
}else if(intent.getAction().equals("android.intent.action.STATIC_BROADCAST")){
String msg1 = intent.getStringExtra("msg");
Log.i(TAG, msg1);
}else if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//当系统收到短信时,会发出一个action名称为Android.provier.Telephony.SMS_RECEIVED的广播Intent,
//该Intent存放了接收到的短信内容,使用名称 "pdus"即可从Intent中获取短信内容。
//pdus是一个object类型的数组,每一个object都是一个byte[]字节数组,每一项为一条短信。
Object[] pduses= (Object[])intent.getExtras().get("pdus");
for(Object pdus: pduses){
byte[] pdusmessage = (byte[])pdus;
SmsMessage sms = SmsMessage.createFromPdu(pdusmessage);
String mobile = sms.getOriginatingAddress();//发送短信的手机号码
String content = sms.getMessageBody(); //短信内容
Date date = new Date(sms.getTimestampMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(date); //得到发送时间
Log.i(TAG, TAG+"-------msm---------");
Log.i(TAG, TAG+"-->mobile:"+mobile);
Log.i(TAG, TAG+"-->content:"+content);
Log.i(TAG, TAG+"-->time:"+time);
}
}
}
} </span>
---------------------main .xml ----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/my_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="BroadCastReceiver的学习"
android:gravity="center_vertical|center_horizontal"/>
<Button
android:id="@+id/send_static"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="静态注册" />
<Button
android:id="@+id/send_dynamic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="动态注册" />
</LinearLayout>
点击该应用程序,先按按钮静态注册,再按动态注册,最好按返回键,生成log为:
从log中我们可以学习activity的声明周期及接受广播的运行过程。
从本代码中可以学习到以下指示:
1.android的声明周期;
2.broadcastreceiver的注册方式:静态注册和动态注册两种;
3.android代码的风格,例如把时间监听和获取控件封装起来,使程序易读;
4.获取短信息的内容;