Security guard: Blacklist interception, broadcast and service addition interception, and security guard

Source: Internet
Author: User

Security guard: Blacklist interception, broadcast and service addition interception, and security guard
The previous section describes how to add, delete, and query a database. This section describes how to use broadcast and service interception.


In order to enable the Service and disable the broadcast at the same time, we cannot register the broadcast in the configuration file, which is difficult to manage, so we put the broadcast in the service. Create a service first.

CallSmsSafeService. java

Be sure to register in the configuration file.

As mentioned in the previous article, the service is an activity without any interface, so you need to rewrite it yourself. The Oncreate and Ondestory methods are used to register and destroy the service and broadcast.


// 1. instantiate the broadcast Receiver

receiver = new InnerSmsReceiver();

// 2. register the broadcast Receiver

RegisterReceiver (receiver, new IntentFilter ("android. provider. telephony. SMS_RECEIVED "); // 3. destroy broadcast <pre name = "code" class = "java"> unregisterReceiver (receiver ER );

Whether it is detected or registered in the configuration file is a step, a listening object and a filter action.

Then we define the InnerSmsReceiver to write the operation we want to perform.
Private class InnerSmsReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {// checks whether the SMS is a blacklist number and sets the text message blocking Object [] objs = (Object []) intent. getExtras (). get ("pdus"); for (Object obj: objs) {SmsMessage smsmsmessage = smsMessage. createFromPdu (byte []) obj); // get the text message sender String sender = smsMessage. getOriginatingAddress (); String mode = dao. findMode (sender); if ("2 ". equals (mod E) | "3". equals (mode) {abortBroadcast () ;}// DEMO code. String body = smsMessage. getMessageBody (); if (body. contains ("fapiao") {// your header invoice is highlighted by language Segmentation technology. AbortBroadcast ();}}}}


Then listen for incoming calls through the service
// ① Get the telephone management service first
tm= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

// ② Perform registration listening and action
           tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

// ③ Define a service to listen for incoming calls
Private class MyListener extends PhoneStateListener {@ Overridepublic void onCallStateChanged (int state, String incomingNumber) {super. onCallStateChanged (state, incomingNumber); switch (state) {case TelephonyManager. CALL_STATE_RINGING: // zero response. String result = dao. findMode (incomingNumber); if ("1 ". equals (result) | "3 ". equals (result) {endCall () ;}break; default: break ;}}}
Then, use aidl to hang up the phone, because Google has hidden this function since version 1.7.
Public void endCall () {// IBinder iBinder = ServiceManager. getService (TELEPHONY_SERVICE); try {// load the servicemanager bytecode Class clazz = CallSmsSafeService. class. getClassLoader (). loadClass ("android. OS. serviceManager "); Method method = clazz. getDeclaredMethod ("getService", String. class); IBinder ibinder = (IBinder) method. invoke (null, TELEPHONY_SERVICE); ITelephony. stub. asInterface (ibinder ). endCall ();} catch (Exception e) {e. printStackTrace ();}}


Remember to destroy the service in Ondestory 

Tm. listen (listener, PhoneStateListener. LISTEN_NONE );

Next address

Trainer Item (3) security guard-blacklist interception using content providers to delete call records

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.