暗扣方式之一:短信訂購,即監聽--------攔截------------處理短信。
暗扣方式之二:模擬人為操作(又叫模擬流量),通過後台程序代碼模擬人的點擊行為,暗自給用戶訂購業務,由運營商收取你的費用,當然這其中也需要涉及監聽/攔截/處理短信。使用這種方式的原理無非是Http處理網頁,還涉及接入點切換問題,這裡就不詳細講解。
暗扣,強烈譴責這種侵害用戶利益的行為。。。
回歸正題:有的時候,我們的手機程序需要監聽手機短信,當滿足條件A時,不處理;當滿足條件B時,將其設置為已讀;當滿足條件C時,將短信刪除。
注:Android 4.3以及以後可能由用戶來控制程序的權限,如果用戶關閉這個程序的權限,意味著你無法監聽短信/操作短信內容
目前也有如小米系統在安裝時,讓用戶來控制權限;360來監聽優先攔截短信等等(關於誰先安裝誰有優先權,動態注冊比靜態注冊優先級別高等問題,在這些情況這裡就不講了)。。。
一,攔截廣播
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
/**
* 短信廣播
* @author Admin
*
*/
public class SmsReceiver extends BroadcastReceiver {
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(SMS_RECEIVED_ACTION))
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] objArray = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[objArray.length];
for (int i = 0; i < objArray.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) objArray[i]);
}
String phoneNum="";//電話號碼
StringBuilder sb = new StringBuilder();//短信內容
for (SmsMessage currentMessage : messages)
{
phoneNum=currentMessage.getDisplayOriginatingAddress();
sb.append(currentMessage.getDisplayMessageBody());
}
//根據你策略的手機端口號/攔截關鍵字判斷進行是否中斷廣播(this.abortBroadcast();),
//TODO
}
}
}
}
二、開啟一個服務開監聽數據庫
import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.os.Process;
/**
* @author Administrator
*
*/
public class SmsService extends Service {
private SmsObserver mObserver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//在這裡啟動
ContentResolver resolver = getContentResolver();
mObserver = new SmsObserver(resolver, new SmsHandler(this));
resolver.registerContentObserver(Uri.parse("content://sms"), true, mObserver);
}
@Override
public void onDestroy() {
super.onDestroy();
this.getContentResolver().unregisterContentObserver(mObserver);
Process.killProcess(Process.myPid());
}
}
三、數據庫觀察者:
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Message;
/**
* @author Administrator
*
*/
public class SmsObserver extends ContentObserver{
private ContentResolver mResolver;
public SmsHandler smsHandler;
public SmsObserver(ContentResolver mResolver,SmsHandler handler) {
super(handler);
this.mResolver=mResolver;
this.smsHandler=handler;
}
@Override
public void onChange(boolean selfChange) {
Cursor mCursor=mResolver.query(Uri.parse("content://sms/inbox"), new String[] { "_id", "address", "read", "body", "thread_id" }, "read=?", new String[] { "0" }, "date desc");
if(mCursor==null)
{
return;
}
else
{
while (mCursor.moveToNext())
{
SmsInfo _smsInfo=new SmsInfo();
int _inIndex=mCursor.getColumnIndex("_id");
if(_inIndex!=-1)
{
_smsInfo._id=mCursor.getString(_inIndex);
}
int thread_idIndex=mCursor.getColumnIndex("thread_id");
if(thread_idIndex!=-1)
{
_smsInfo.thread_id=mCursor.getString(thread_idIndex);
}
int addressIndex=mCursor.getColumnIndex("address");
if(addressIndex!=-1)
{
_smsInfo.smsAddress=mCursor.getString(addressIndex);
}
int bodyIndex=mCursor.getColumnIndex("body");
if(bodyIndex!=-1)
{
_smsInfo.smsBody=mCursor.getString(bodyIndex);
}
int readIndex=mCursor.getColumnIndex("read");
if(readIndex!=-1)
{
_smsInfo.read=mCursor.getString(readIndex);
}
//根據你的攔截策略,判斷是否不對短信進行操作;將短信設置為已讀;將短信刪除
//TODO
Message msg=smsHandler.obtainMessage();
_smsInfo.action=2;//0不對短信進行操作;1將短信設置為已讀;2將短信刪除
msg.obj=_smsInfo;
smsHandler.sendMessage(msg);
}
}
if(mCursor!=null)
{
mCursor.close();
mCursor=null;
}
}
}
四、短信處理類
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
/**
* @author 短信的處理
*
*/
public class SmsHandler extends Handler
{
private Context mcontext;
public SmsHandler(Context context)
{
this.mcontext=context;
}
@Override
public void handleMessage(Message msg)
{
SmsInfo smsInfo=(SmsInfo)msg.obj;
if(smsInfo.action==1)
{
ContentValues values = new ContentValues();
values.put("read", "1");
mcontext.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "thread_id=?", new String[]{smsInfo.thread_id});
}
else if(smsInfo.action==2)
{
Uri mUri=Uri.parse("content://sms/");
mcontext.getContentResolver().delete(mUri, "_id=?", new String[]{smsInfo._id});
}
}
}
五、SmsInfo 數據結構
/**
* 主要用於短信攔截
* @author Administrator
*
*/
public class SmsInfo {
public String _id="";
public String thread_id = "";
public String smsAddress = "";
public String smsBody = "";
public String read="";
public int action=0;//1代表設置為已讀,2表示刪除短信
}
短信攔截大概就是如此了。
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11