[Android] mobile guard blacklist function (SMS interception), android guard
Previously, we stored and displayed all the mobile phone numbers to be intercepted. Next, we used the broadcast receiver to intercept text messages. This broadcast receiver needs to be bound to a service. when the service is enabled, the receiver exists, when the service is stopped, the receiver closes
Define a class CallSmsSafeService under the service package to inherit the system's Service
Override the onCreate () method
Obtain the BroadcastReceiver object.
Call the registerReceiver () method to register the broadcast. parameters: BroadcastReceiver object and IntentFillter object
Override onDestory () method
Call the unregisterReceiver () method. Parameter: BroadcastReceiver object
The BroadcastReceiver object is set to null.
Defines an InnerSmsReceiver class that inherits the BroadcastReceiver of the system.
Override the onReceive () method and pass in parameters: Context object, Intent object
Call the getExtras (). get ("pdus") method of the Intent Object to obtain the Object [] array.
For Loop Object [] array, each of which is an Object
Call the SmsMessage. createFromPdu () method to obtain the SmsMessage Object. The parameter is an array of byte [] and is strongly converted to an Object.
Call the getOriginatingAddress () method of the SmsMessage object to obtain the String sender.
Call the database query method of the Dao object to find the interception mode of this text message.
Determine the interception mode. If all requests are intercepted 3 or 2, call the abortBroadcast () method.
Configure the service in the configuration center to enable and disable the service.
View this: http://www.cnblogs.com/taoshihan/p/5468523.html
CallSmsSafeService. java
Package com. qingguow. mobilesafe. service; import android. app. service; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. IBinder; import android. telephony. smsMessage; import com. qingguow. mobilesafe. db. ado. blackNumberAdo;/*** SMS phone interception service * @ author taoshihan **/public class CallSmsSafeService extends Se Rvice {private InnerSmsReceiver receiver; private BlackNumberAdo ado; @ Override public IBinder onBind (Intent intent) {// TODO Auto-generated method stub return null ;} /*** service creation */@ Override public void onCreate () {explorer = new InnerSmsReceiver (); IntentFilter filter = new IntentFilter ("android. provider. telephony. SMS_RECEIVED "); ado = new BlackNumberAdo (getApplicationContext (); registerReceiver (r Eceiver, filter); super. onCreate ();}/*** receive broadcast * @ author taoshihan **/private class InnerSmsReceiver extends BroadcastReceiver {@ Override public void onReceive (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 (send Er); if (mode! = Null) {if (mode. equals ("2") | mode. equals ("3") {System. out. println ("intercept SMS:" + sender); abortBroadcast () ;}}}}@ Override public void onDestroy () {unregisterReceiver (assumer); Referer = null; super. onDestroy ();}}
Ado
Package com. qingguow. mobilesafe. db. ado; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. content. contentValues; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import com. qingguow. mobilesafe. db. blackNumberDBOpenHelper; public class BlackNumberAdo {private BlackNumberDBOpenHelper he Lper; public BlackNumberAdo (Context context) {helper = new BlackNumberDBOpenHelper (context );} /*** insert data ** @ param phone * @ param mode */public void add (String phone, String mode) {SQLiteDatabase db = helper. getWritableDatabase (); ContentValues values = new ContentValues (); values. put ("phone", phone); values. put ("mode", mode); db. insert ("blacknumber", null, values); db. close ();}/*** query all * * @ Return */public List <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 = new ArrayList <Map <String, string >>(); while (cursor. moveToNext () {Map <String, String> info = new HashMap <String, String> (); String phone = cursor. getString (0); Strin G mode = cursor. getString (1); info. put ("phone", phone); info. put ("mode", mode); list. add (info);} cursor. close (); db. close (); return list;}/*** modify data ** @ param phone * @ param mode */public void update (String phone, String mode) {SQLiteDatabase db = helper. getWritableDatabase (); ContentValues values = new ContentValues (); values. put ("phone", phone); values. put ("mode", mode); db. update ("blacknu Mber ", values," phone =? ", New String [] {phone}); db. close ();}/*** delete data ** @ param phone */public void delete (String phone) {SQLiteDatabase db = helper. getWritableDatabase (); db. delete ("blacknumber", "phone =? ", New String [] {phone}); db. close ();}/*** find a single record ** @ param phone */public String find (String phone) {SQLiteDatabase db = helper. getReadableDatabase (); Cursor cursor = db. rawQuery ("select mode from blacknumber where phone =? ", New String [] {phone}); if (cursor. moveToNext () {String mode = cursor. getString (0); return mode ;}return null ;}}