android通过BroadcastReceiver拦截短信转发
通过BroadcastReceiver拦截短信转发,技术讨论 大家不要做坏事哦
先看布局:
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <EditText
8 android:id="@+id/sendToId"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:paddingLeft="5dp"
12 android:layout_marginLeft="10dp"
13 android:layout_marginRight="10dp"
14 android:layout_marginTop="5dp"
15 android:inputType="phone"
16 android:hint="填写需要转发到的号码"
17 />
18 <RelativeLayout
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:orientation="horizontal">
22 <TextView
23 android:layout_width="wrap_content"
24 android:layout_height="wrap_content"
25 android:layout_marginTop="10dp"
26 android:layout_marginLeft="10dp"
27 android:text="是否开启本机接收:"
28 />
29 <ToggleButton
30 android:id="@+id/togId"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:layout_alignParentRight="true"
34 android:layout_marginRight="20dp"
35 android:textOn="ON"
36 android:textOff="OFF"
37 />
38 </RelativeLayout>
39 <Button
40 android:id="@+id/saveBtId"
41 android:layout_width="100dp"
42 android:layout_height="wrap_content"
43 android:layout_gravity="center_horizontal"
44 android:text="确定"
45 />
46
47 </LinearLayout>
主Activity:
View Code
1 package tiantian.Package;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.widget.Button;
8 import android.widget.CompoundButton;
9 import android.widget.CompoundButton.OnCheckedChangeListener;
10 import android.widget.EditText;
11 import android.widget.Toast;
12 import android.widget.ToggleButton;
13
14 public class SMSReceiverActivity extends Activity {
15 /** Called when the activity is first created. */
16 private EditText et;
17 private Button saveBt;
18 private ToggleButton toBt;
19 private boolean isChecked = false;
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.main);
24 et = (EditText) findViewById(R.id.sendToId);
25 toBt = (ToggleButton) findViewById(R.id.togId);
26 toBt.setOnCheckedChangeListener(new OnCheckedChangeListener() {
27
28 @Override
29 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
30 // TODO Auto-generated method stub
31 if(isChecked){
32 SMSReceiverActivity.this.isChecked = isChecked;
33 }
34 }
35 });
36 saveBt = (Button) findViewById(R.id.saveBtId);
37 saveBt.setOnClickListener(new View.OnClickListener() {
38
39 @Override
40 public void onClick(View v) {
41 // TODO Auto-generated method stub
42 Intent intent = new Intent();
43 intent.setAction("com.tiantian.SEND_TO");
44 intent.putExtra("_sendTo", et.getText().toString());
45 intent.putExtra("_isChecked", isChecked);
46 sendBroadcast(intent);
47 Toast.makeText(SMSReceiverActivity.this, "已成功绑定", Toast.LENGTH_SHORT).show();
48 }
49 });
50
51 }
52
53
54 }
BroadcastReceiver类继承BroadcastReceiver:
View Code
1 package tiantian.Package;
2
3 import java.sql.Date;
4 import java.text.SimpleDateFormat;
5
6 import android.content.BroadcastReceiver;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.telephony.SmsManager;
10 import android.telephony.SmsMessage;
11
12 public class SMSReceiverSend extends BroadcastReceiver{
13 static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
14 private static String sendToNum = "1234";
15 private static boolean isChecked = false;
16 @Override
17 public void onReceive(Context content, Intent intent) {
18 // TODO Auto-generated method stub
19 if(intent.getAction().equals("com.tiantian.SEND_TO")){
20 sendToNum = intent.getStringExtra("_sendTo");
21 isChecked = intent.getBooleanExtra("_isChecked", isChecked);
22 }else if(intent.getAction().equals(SMS_ACTION)){
23
24 Object[] pdus = (Object[])intent.getExtras().get("pdus");
25 if(pdus != null && pdus.length != 0){
26 SmsMessage[] messages = new SmsMessage[pdus.length];
27 for(int i=0;i<pdus.length;i++){
28 byte[] pdu = (byte[])pdus[i];
29 messages[i] = SmsMessage.createFromPdu(pdu);
30 }
31 for(SmsMessage message : messages){
32 String messageBody = message.getMessageBody();
33 String sender = message.getOriginatingAddress();
34 if(isChecked == false){
35 abortBroadcast();// 中止发送
36 }
37 Date date = new Date(message.getTimestampMillis());
38 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
39 String sendContent = "date:" + format.format(date) + "\n"
40 + "sender:" + sender + "\n" + "messageBody:" + messageBody;
41 SmsManager smsManager = SmsManager.getDefault();
42 smsManager.sendTextMessage(sendToNum, null, sendContent, null, null);
43 }
44 }
45
46 }
47 }
48
49 }
AndroidManifest.xml:
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="tiantian.Package"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="8" />
8
9 <application
10 android:icon="@drawable/ic_launcher"
11 android:label="@string/app_name" >
12 <activity
13 android:label="@string/app_name"
14 android:name=".SMSReceiverActivity" >
15 <intent-filter >
16 <action android:name="android.intent.action.MAIN" />
17
18 <category android:name="android.intent.category.LAUNCHER" />
19 </intent-filter>
20 </activity>
21
22 <receiver android:label="@string/app_name"
23 android:name=".SMSReceiverSend">
24 <intent-filter android:priority="1000">
25 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
26 <action android:name="com.tiantian.SEND_TO"/>
27 <category android:name="android.intent.category.DEFAULT" />
28 </intent-filter>
29 </receiver>
30
31 </application>
32 <uses-permission android:name="android.permission.RECEIVE_SMS"/>
33 <uses-permission android:name="android.permission.SEND_SMS"/>
34 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
35 </manifest>