Before we put the mobile phone number to be intercepted and displayed, the next is to use the broadcast receiver to intercept the text message, the broadcast receiver needs and a service binding, when the service is open, the receiver exists, when the service is stopped, the receiver shuts down
define a class under the service package Callsmssafeservice The service of the inheriting system
Overriding the onCreate () method
Get broadcastreceiver object,new out
Call the registerreceiver () method, register the broadcast, parameters:broadcastreceiver object,intentfillter Object
Overriding the ondestory () method
Call the unregisterreceiver () method, Parameter:broadcastreceiver Object
Broadcastreceiver object is set to null
Define an internal class Innersmsreceiver inherit the broadcastreceiver of the system
Override the onreceive () method, pass in Parameters:Context object,Intent Object
Call the Getextras (). Get ("PDUs") method of the Intent object to get to the object[] Array
For Loop object[] Array, each of the Object Objects
Call the SMSMESSAGE.CREATEFROMPDU () method, get to the smsmessage object, parameter:byte[] Array, Strong-Turn Object Object
Call the getoriginatingaddress () method of the smsmessage object to get the String Sender
Call the Dao object's query database method to find out the interception mode of this text message
To determine the interception mode, if it is all intercept 3 or SMS interception 2, call the abortbroadcast () method
Configure the above services in the Settings center to enable and disable functions
View this article:http://www.cnblogs.com/taoshihan/p/5468523.html
Callsmssafeservice.java
PackageCom.qingguow.mobilesafe.service;ImportAndroid.app.Service;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.IBinder;ImportAndroid.telephony.SmsMessage;ImportCom.qingguow.mobilesafe.db.ado.BlackNumberAdo;/*** Short-term telephone interception service *@authorTaoshihan **/ Public classCallsmssafeserviceextendsService {Privateinnersmsreceiver Receiver; PrivateBlacknumberado ADO; @Override Publicibinder onbind (Intent Intent) {//TODO auto-generated Method Stub return NULL; } /*** Service Creation*/@Override Public voidonCreate () {receiver=NewInnersmsreceiver (); Intentfilter Filter=NewIntentfilter ("Android.provider.Telephony.SMS_RECEIVED"); ADO=NewBlacknumberado (Getapplicationcontext ()); Registerreceiver (receiver, filter); Super. OnCreate (); } /*** Receive broadcast *@authorTaoshihan **/ Private classInnersmsreceiverextendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {object[] Objs= (object[]) Intent.getextras (). Get ("PDUs"); for(Object obj:objs) {smsmessage SMS=SMSMESSAGE.CREATEFROMPDU ((byte[]) obj); String Sender=sms.getoriginatingaddress (); String Mode=Ado.find (sender); if(mode!=NULL){ if(Mode.equals ("2") | | mode.equals ("3") {System.out.println ("Intercept SMS:" +sender); Abortbroadcast (); } }}} @Override Public voidOnDestroy () {unregisterreceiver (receiver); Receiver=NULL; Super. OnDestroy (); }}
ADO Class
PackageCom.qingguow.mobilesafe.db.ado;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importandroid.content.ContentValues;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportCom.qingguow.mobilesafe.db.BlackNumberDBOpenHelper; Public classBlacknumberado {PrivateBlacknumberdbopenhelper Helper; PublicBlacknumberado (Context context) {Helper=NewBlacknumberdbopenhelper (context); } /*** Insert Data * *@paramPhone *@paramMode*/ Public voidAdd (String phone, string mode) {Sqlitedatabase db=helper.getwritabledatabase (); Contentvalues Values=Newcontentvalues (); Values.put ("Phone", phone); Values.put ("Mode", mode); Db.insert ("Blacknumber",NULL, values); Db.close (); } /*** Search All * *@return */ PublicList<map<string, string>>FindAll () {Sqlitedatabase db=helper.getreadabledatabase (); Cursor Cursor=Db.rawquery ("Select Phone,mode from Blacknumber ORDER by id DESC",NULL); List<map<string, string>> list =NewArraylist<map<string, string>>(); while(Cursor.movetonext ()) {Map<string, string> info =NewHashmap<string, string>(); String Phone= cursor.getstring (0); String Mode= cursor.getstring (1); Info.put ("Phone", phone); Info.put ("Mode", mode); List.add (info); } cursor.close (); Db.close (); returnlist; } /*** Change Data * *@paramPhone *@paramMode*/ Public voidUpdate (string phone, string mode) {Sqlitedatabase db=helper.getwritabledatabase (); Contentvalues Values=Newcontentvalues (); Values.put ("Phone", phone); Values.put ("Mode", mode); Db.update ("Blacknumber", Values, "phone=?",Newstring[] {phone}); Db.close (); } /*** Delete Data * *@paramPhone*/ Public voidDelete (String phone) {sqlitedatabase db=helper.getwritabledatabase (); Db.delete ("Blacknumber", "phone=?",Newstring[] {phone}); Db.close (); } /*** Find a single * *@paramPhone*/ Publicstring Find (String phone) {Sqlitedatabase db=helper.getreadabledatabase (); Cursor Cursor=Db.rawquery ("Select mode from Blacknumber where phone=?", Newstring[] {phone}); if(Cursor.movetonext ()) {String mode= cursor.getstring (0); returnmode; } return NULL; }}
[Android] Mobile defender blacklist feature (SMS blocker)