[Android] phone defender Caller ID number attribution

Source: Internet
Author: User

Continue the project N days ago

Turn on the service monitor phone calls, query the database, show the place of attribution

For more information, refer to this blog post:http://www.cnblogs.com/taoshihan/p/5331232.html

Addressservice.java

 PackageCom.qingguow.mobilesafe.service;ImportCom.qingguow.mobilesafe.utils.NumberQueryAddressUtil;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.telephony.PhoneStateListener;ImportAndroid.telephony.TelephonyManager;ImportAndroid.widget.Toast;/*** Call Display * *@authorTaoshihan **/ Public classAddressserviceextendsService {PrivateTelephonymanager TM; PrivateMyphonestatelistener Phonestatelistener; @Override Publicibinder onbind (Intent arg0) {//TODO auto-generated Method Stub        return NULL; }    /*** Service Creation*/@Override Public voidonCreate () {Super. OnCreate (); TM=(Telephonymanager) Getsystemservice (Telephony_service); Phonestatelistener=NewMyphonestatelistener ();    Tm.listen (Phonestatelistener, phonestatelistener.listen_call_state); }    Private classMyphonestatelistenerextendsPhonestatelistener {@Override Public voidOncallstatechanged (intState , String Incomingnumber) {            Super. oncallstatechanged (State, Incomingnumber); Switch(state) { CaseTelephonyManager.CALL_STATE_RINGING:String Info=numberqueryaddressutil. queryaddress (Incomingnumber); Toast.maketext (Getapplicationcontext (), info,1). Show ();  Break; default:                 Break; }        }    }    /*** Service Destruction*/@Override Public voidOnDestroy () {//TODO auto-generated Method Stub        Super. OnDestroy (); //Cancel MonitoringTm.listen (Phonestatelistener, Phonestatelistener.listen_none); Phonestatelistener=NULL; }}

Set Center, configure whether to turn on caller attribution display

Directly using the combined controls we defined earlier

    <Com.qingguow.mobilesafe.ui.SettingItemViewTsh:title= "Set display number to attribution"tsh:desc_on= "Set display number attribution is turned on"Tsh:desc_off= "Set display number attribution closed"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/siv_show_address">    </Com.qingguow.mobilesafe.ui.SettingItemView>

Get to the settingitemview object, our custom control, set the state

Call the Setonclicklistener () method of the settingitemview Object , set the Click event, override the OnClick Method

Call the isChecked () method of the settingitemview object to get the currently selected

Determine the state, call the setchecked () method of the settingitemview Object , set the state, parameter: Boolean

Call the StartService () method to turn on the service that monitors the phone's status, parameters:Intent object,

Call the StopService () method to close the service

Determines whether the current service is turned on, sets the default check state of the control

Create a new tool class Servicesutils.java

Define a static method isservicerunning (), incoming parameters: Context context,String service name

Call the getsystemservice () method of the Context object to get the activitymanager object, Parameters: Context.activity_service

Call the getrunningservices () method of the Activitymanager object to get the running service List Collection, Parameters: int Maximum value

For Loop List Collection, each of which is a runningserviceinfo Object

Call RunningServiceInfo.servie.getClassName (), get to the String service class name, and judge if Equality returns True

Settingactivity.java

 PackageCom.qingguow.mobilesafe;Importandroid.app.Activity;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportCom.qingguow.mobilesafe.service.AddressService;ImportCom.qingguow.mobilesafe.ui.SettingItemView;Importcom.qingguow.mobilesafe.utils.ServiceUtils; Public classSettingactivityextendsActivity {PrivateSettingitemview Siv_item; PrivateSharedpreferences sp; //set whether to turn on number attribution    PrivateSettingitemview showaddressbtn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_setting); //set a number to attributionSHOWADDRESSBTN =(Settingitemview) Findviewbyid (r.id.siv_show_address); if(Serviceutils.isrunningservice ( This,                "Com.qingguow.mobilesafe.service.AddressService") {showaddressbtn.setchecked (true); } Else{showaddressbtn.setchecked (false); } showaddressbtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {if(showaddressbtn.ischecked ()) {showaddressbtn.setchecked (false); StopService (NewIntent (Getapplicationcontext (), Addressservice.class)); } Else{showaddressbtn.setchecked (true); StartService (NewIntent (Getapplicationcontext (), Addressservice.class));        }            }        }); Siv_item=(Settingitemview) Findviewbyid (R.id.siv_item); SP= getsharedpreferences ("config", mode_private); //set the state based on the saved data        BooleanUpdate = Sp.getboolean ("Update",false); if(update) {siv_item.setchecked (true); } Else{siv_item.setchecked (false); }        //Auto-Updated click eventsSiv_item.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {Editor Editor=Sp.edit (); if(siv_item.ischecked ()) {//settings are not selectedSiv_item.setchecked (false); Editor.putboolean ("Update",false); } Else {                    //Set the selectedSiv_item.setchecked (true); Editor.putboolean ("Update",true);            } editor.commit ();    }        }); }}

Servicesutils.java

 Packagecom.qingguow.mobilesafe.utils;Importjava.util.List;ImportAndroid.app.ActivityManager;ImportAndroid.app.ActivityManager.RunningServiceInfo;ImportAndroid.content.Context;/*** Service Tool class *@authorTaoshihan **/ Public classServiceutils {/*** Determine if a service is turned on *@paramContext *@paramServiceName *@return     */     Public Static BooleanIsrunningservice (Context context,string serviceName) {Activitymanager am=(Activitymanager) Context.getsystemservice (Context.activity_service); List<RunningServiceInfo> infos=am.getrunningservices (100);  for(Runningserviceinfo info:infos) {String name=Info.service.getClassName (); if(Name.equals (serviceName)) {return true; }        }        return false; }}

[Android] phone defender Caller ID number attribution

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.