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>
5 使用模拟器发送短信, sms是短信,voice是电话
window--->show view ---> other---->android -----> emulator control
6 效果图
主页面没啥变化,
<?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 效果图
相关文章推荐
- android手机短信监听器,监听到的短信发送到web服务器,完整源码分享
- [Android实例] android如何监听发送短信?
- Android--调系统发短信界面以及监听短信发送
- Android 简单发送短信实例
- 在Android中发送短信和彩信,监听短信并显示
- Android 监听短信内容变化,并发送到自己的手机
- Android 短信转换成彩信的消息数量(实例代码)
- android如何监听发送短信?
- Android短信的发送和广播接收者实现短信的监听
- Android串口通讯,在线程里一直监听是否有消息发来,接收到消息到runOnUiThread处理跳转页面,页面跳转关闭之后,在发送消息尽然还会执行在打开页面?
- Android短信发送,监听,及其工具类封装
- android广播监听接收和发送短信
- Android发送短信方法实例详解
- Android短信的发送和广播接收者实现短信的监听
- android发送短信并监听插入收件箱的方法
- [Android实例] android 发送短信和打电话
- 详解Android短信的发送和广播接收实现短信的监听
- android发送短信以及监听发送状态
- Android 程式开发:(廿一)消息传递 —— 21.3 使用Intent发送短信
- Android开发之监听或获取手机短信内容 分类: Android 消息 2015-04-20 19:23 38人阅读 评论(0) 收藏