标签:
暗扣方式之一:短信订购,即监听--------拦截------------处理短信。
暗扣方式之二:模拟人为操作(又叫模拟流量),通过后台程序代码模拟人的点击行为,暗自给用户订购业务,由运营商收取你的费用,当然这其中也需要涉及监听/拦截/处理短信。使用这种方式的原理无非是Http处理网页,还涉及接入点切换问题,这里就不详细讲解。
暗扣,强烈谴责这种侵害用户利益的行为。。。
回归正题:有的时候,我们的手机程序需要监听手机短信,当满足条件A时,不处理;当满足条件B时,将其设置为已读;当满足条件C时,将短信删除。
注:Android 4.3以及以后可能由用户来控制程序的权限,如果用户关闭这个程序的权限,意味着你无法监听短信/操作短信内容
目前也有如小米系统在安装时,让用户来控制权限;360来监听优先拦截短信等等(关于谁先安装谁有优先权,动态注册比静态注册优先级别高等问题,在这些情况这里就不讲了)。。。
一,拦截广播
1 import android.content.BroadcastReceiver; 2 import android.content.Context; 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.telephony.SmsMessage; 6 7 /** 8 * 短信广播 9 * @author Admin 10 * 11 */ 12 public class SmsReceiver extends BroadcastReceiver { 13 14 public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; 15 16 @Override 17 public void onReceive(Context context, Intent intent) 18 { 19 if (intent.getAction().equals(SMS_RECEIVED_ACTION)) 20 { 21 Bundle bundle = intent.getExtras(); 22 if (bundle != null) 23 { 24 Object[] objArray = (Object[]) bundle.get("pdus"); 25 SmsMessage[] messages = new SmsMessage[objArray.length]; 26 for (int i = 0; i < objArray.length; i++) 27 { 28 messages[i] = SmsMessage.createFromPdu((byte[]) objArray[i]); 29 } 30 31 String phoneNum="";//电话号码 32 StringBuilder sb = new StringBuilder();//短信内容 33 34 for (SmsMessage currentMessage : messages) 35 { 36 phoneNum=currentMessage.getDisplayOriginatingAddress(); 37 sb.append(currentMessage.getDisplayMessageBody()); 38 } 39 40 //根据你策略的手机端口号/拦截关键字判断进行是否中断广播(this.abortBroadcast();), 41 //TODO 42 } 43 } 44 } 45 }
二、开启一个服务开监听数据库
1 import android.app.Service; 2 import android.content.ContentResolver; 3 import android.content.Intent; 4 import android.net.Uri; 5 import android.os.IBinder; 6 import android.os.Process; 7 8 /** 9 * @author Administrator 10 * 11 */ 12 public class SmsService extends Service { 13 14 private SmsObserver mObserver; 15 16 @Override 17 public IBinder onBind(Intent intent) { 18 return null; 19 } 20 21 @Override 22 public void onCreate() { 23 //在这里启动 24 ContentResolver resolver = getContentResolver(); 25 mObserver = new SmsObserver(resolver, new SmsHandler(this)); 26 resolver.registerContentObserver(Uri.parse("content://sms"), true, mObserver); 27 } 28 29 @Override 30 public void onDestroy() { 31 super.onDestroy(); 32 this.getContentResolver().unregisterContentObserver(mObserver); 33 Process.killProcess(Process.myPid()); 34 } 35 }
三、数据库观察者:
1 import android.content.ContentResolver; 2 import android.database.ContentObserver; 3 import android.database.Cursor; 4 import android.net.Uri; 5 import android.os.Message; 6 7 8 /** 9 * @author Administrator 10 * 11 */ 12 public class SmsObserver extends ContentObserver{ 13 14 private ContentResolver mResolver; 15 public SmsHandler smsHandler; 16 17 public SmsObserver(ContentResolver mResolver,SmsHandler handler) { 18 super(handler); 19 this.mResolver=mResolver; 20 this.smsHandler=handler; 21 } 22 23 @Override 24 public void onChange(boolean selfChange) { 25 Cursor mCursor=mResolver.query(Uri.parse("content://sms/inbox"), new String[] { "_id", "address", "read", "body", "thread_id" }, "read=?", new String[] { "0" }, "date desc"); 26 27 if(mCursor==null) 28 { 29 return; 30 } 31 else 32 { 33 while (mCursor.moveToNext()) 34 { 35 SmsInfo _smsInfo=new SmsInfo(); 36 37 int _inIndex=mCursor.getColumnIndex("_id"); 38 if(_inIndex!=-1) 39 { 40 _smsInfo._id=mCursor.getString(_inIndex); 41 } 42 43 int thread_idIndex=mCursor.getColumnIndex("thread_id"); 44 if(thread_idIndex!=-1) 45 { 46 _smsInfo.thread_id=mCursor.getString(thread_idIndex); 47 } 48 49 int addressIndex=mCursor.getColumnIndex("address"); 50 if(addressIndex!=-1) 51 { 52 _smsInfo.smsAddress=mCursor.getString(addressIndex); 53 } 54 55 int bodyIndex=mCursor.getColumnIndex("body"); 56 if(bodyIndex!=-1) 57 { 58 _smsInfo.smsBody=mCursor.getString(bodyIndex); 59 } 60 61 int readIndex=mCursor.getColumnIndex("read"); 62 if(readIndex!=-1) 63 { 64 _smsInfo.read=mCursor.getString(readIndex); 65 } 66 67 //根据你的拦截策略,判断是否不对短信进行操作;将短信设置为已读;将短信删除 68 //TODO 69 Message msg=smsHandler.obtainMessage(); 70 _smsInfo.action=2;//0不对短信进行操作;1将短信设置为已读;2将短信删除 71 msg.obj=_smsInfo; 72 smsHandler.sendMessage(msg); 73 } 74 } 75 76 if(mCursor!=null) 77 { 78 mCursor.close(); 79 mCursor=null; 80 } 81 } 82 }
四、短信处理类
1 import android.content.ContentValues; 2 import android.content.Context; 3 import android.net.Uri; 4 import android.os.Handler; 5 import android.os.Message; 6 7 /** 8 * @author 短信的处理 9 * 10 */ 11 public class SmsHandler extends Handler 12 { 13 private Context mcontext; 14 15 public SmsHandler(Context context) 16 { 17 this.mcontext=context; 18 } 19 20 @Override 21 public void handleMessage(Message msg) 22 { 23 SmsInfo smsInfo=(SmsInfo)msg.obj; 24 25 if(smsInfo.action==1) 26 { 27 ContentValues values = new ContentValues(); 28 values.put("read", "1"); 29 mcontext.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "thread_id=?", new String[]{smsInfo.thread_id}); 30 } 31 else if(smsInfo.action==2) 32 { 33 Uri mUri=Uri.parse("content://sms/"); 34 mcontext.getContentResolver().delete(mUri, "_id=?", new String[]{smsInfo._id}); 35 } 36 } 37 }
五、SmsInfo 数据结构
1 /** 2 * 主要用于短信拦截 3 * @author Administrator 4 * 5 */ 6 public class SmsInfo { 7 public String _id=""; 8 public String thread_id = ""; 9 public String smsAddress = ""; 10 public String smsBody = ""; 11 public String read=""; 12 public int action=0;//1代表设置为已读,2表示删除短信 13 }
短信拦截大概就是如此了。
标签:
原文地址:http://www.cnblogs.com/chenlong-50954265/p/4499506.html