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

Android监听器之监听短信发送消息实例

2013-08-24 10:38 357 查看
1

主页面没啥变化,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>



2
主类也没啥 就是设置了一个字符串

package dfzy.EX042;

import dfzy.EX042.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class EX042 extends Activity
{
private TextView mTextView1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*通过findViewById构造器创建TextView对象*/
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mTextView1.setText("等待中...");
}
}


3 添加广播
package dfzy.EX042;

/*必须引用BroadcastReceiver类*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
/*必须引用telephoney.gsm.SmsMessage来收取短信*/
import android.telephony.gsm.SmsMessage;
/*必须引用Toast类来告知用户收到短信*/
import android.widget.Toast;

/* 自定义继承自BroadcastReceiver类,聆听系统服务广播的信息 */
public class EX042_SMS extends BroadcastReceiver
{
/*
* 声明静态字符串,并使用android.provider.Telephony.SMS_RECEIVED 作为Action为短信的依据
*/
private static final String mACTION = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
/* 判断传来Intent是否为短信 */
if (intent.getAction().equals(mACTION))
{
/* 建构一字符串集合变量sb */
StringBuilder sb = new StringBuilder();
/* 接收由Intent传来的数据 */
Bundle bundle = intent.getExtras();
/* 判断Intent是有数据 */
if (bundle != null)
{
/*
* pdus为 android内置短信参数 identifier 通过bundle.get("")返回一包含pdus的对象
*/
Object[] myOBJpdus = (Object[]) bundle.get("pdus");
/* 构建短信对象array,并依据收到的对象长度来创建array的大小 */
SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
for (int i = 0; i < myOBJpdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);
}

/* 将送来的短信合并自定义信息于StringBuilder当中 */
for (SmsMessage currentMessage : messages)
{
sb.append("正在接收到来自:\n");
/* 来讯者的电话号码 */
sb.append(currentMessage.getDisplayOriginatingAddress());
sb.append("\n------发来的短信------\n");
/* 取得传来信息的BODY */
sb.append(currentMessage.getDisplayMessageBody());
}
}
/* 以Notification(Toase)显示来讯信息 */
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();

/* 返回主Activity */
Intent i = new Intent(context, EX042.class);
/* 设置让以一个全新的task来运行 */
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}


4 开通权限
<receiver android:name="dfzy.EX042.EX042_SMS">
<!-- 设置要捕捉的信息名是provider中的Telephony.SMS_RECEIVED -->
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>


5 使用模拟器发送短信, sms是短信,voice是电话
 window--->show view ---> other---->android -----> emulator control



6 效果图

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