[Android] mobile guard receives a call broadcast display number from the home location, android guard

Source: Internet
Author: User

[Android] mobile guard receives a call broadcast display number from the home location, android guard

Use the broadcast receiver to receive the call intention and display the phone number attribution

 

Create an OutCallReceiver class to inherit the BroadcastReceiver OF THE SYSTEM

Override onReceive () method

Call the getResultData () method to obtain the String phone number and query the database to obtain the region

Package com. qingguow. mobilesafe. receiver; import com. qingguow. mobilesafe. utils. numberQueryAddressUtil; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. widget. toast;/*** receive call broadcast * @ author taoshihan **/public class OutcallReceiver extends BroadcastReceiver {@ Override public void onReceive (Context arg0, Intent arg1) {String phone = getResultData (); String address = NumberQueryAddressUtil. queryAddress (phone); System. out. println ("111" + address); Toast. makeText (arg0, address, 1 ). show ();}}

 

 

Define the <worker ER> node and <intent-filter> node in the configuration file, and set the <action> node name, android: name = "android. intent. action. NEW_OUTGOING_CALL"

Permission required, android. permission. process_outgoing_cils

Code registration Broadcast

At this time, the broadcast receiver will be called as long as a call is made, and the user experience is poor. We use the code to register the broadcast receiver and use the Enable listening phone call part of the setup center to control the broadcast.

 

Call the registerReceiver () method in the service to register the broadcast. parameters: BroadcastReceiver object and IntentFilter object

Gets the IntentFilter object and calls the addAction () method of the IntentFilter object. The parameter is android. intent. action. NEW_OUTGOING_CALL.

 

Remove the registration broadcast from the service and call the unregisterReceiver () method. Parameter: BroadcastReceiver object

 

Package com. qingguow. mobilesafe. service; import com. qingguow. mobilesafe. aggreger. outcallcycler; import com. qingguow. mobilesafe. utils. numberQueryAddressUtil; import android. app. service; import android. content. intent; import android. content. intentFilter; import android. OS. IBinder; import android. telephony. phoneStateListener; import android. telephony. telephonyManager; import android. widget. toast;/*** Caller Display ** @ author taoshihan **/public class AddressService extends Service {private TelephonyManager tm; private MyPhoneStateListener phoneStateListener; private OutcallReceiver outcallReceiver; @ Override public IBinder onBind (Intent arg0) {// TODO Auto-generated method stub return null;}/*** service creation */@ Override public void onCreate () {super. onCreate (); tm = (TelephonyManager) getSystemService (TELEPHONY_SERVICE); phoneStateListener = new MyPhoneStateListener (); tm. listen (phoneStateListener, PhoneStateListener. LISTEN_CALL_STATE); // register broadcast outcallReceiver = new OutcallReceiver (); IntentFilter filter = new IntentFilter (); filter. addAction ("android. intent. action. NEW_OUTGOING_CALL "); registerReceiver (outcallReceiver, filter);} private class MyPhoneStateListener extends PhoneStateListener {@ Override public void onCallStateChanged (int state, String incomingNumber) {super. onCallStateChanged (state, incomingNumber); switch (state) {case TelephonyManager. CALL_STATE_RINGING: String info = NumberQueryAddressUtil. queryAddress (incomingNumber); Toast. makeText (getApplicationContext (), info, 1 ). show (); break; default: break; }}/ *** service destruction */@ Override public void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); // cancel listening to tm. listen (phoneStateListener, PhoneStateListener. LISTEN_NONE); phoneStateListener = null; // unregister the broadcast unregisterReceiver (outcallReceiver); outcallReceiver = null ;}}

 

Related Article

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.