Yesterday realized 360 mobile phone Guardian's caller ID to display the function, the specific function is when the caller, display the current number of the place, after learning to find the operation
Very simple, the specific implementation of the code is as follows:
Addressservice.java
Package com.qingguow.mobilesafe.service;
Import Com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.IBinder;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.TelephonyManager;
Import Android.widget.Toast;
/** * Caller ID * * * @author Taoshihan * * */public class Addressservice extends Service {private Telephonymanager TM;
Private Myphonestatelistener Phonestatelistener;
@Override public IBinder onbind (Intent arg0) {//TODO auto-generated the 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); Private class Myphonestatelistener extends Phonestatelistener {@Override public void oncallstatechanged (int StateString 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 destroyed/@Override public void OnDestroy () {//TODO auto-generated method Stub Super
. OnDestroy ();
Cancel the Listening Tm.listen (Phonestatelistener, Phonestatelistener.listen_none);
Phonestatelistener=null;
}
}
Set the center, configure whether to open the caller ID display
Directly using the combined controls that we previously defined
<com.qingguow.mobilesafe.ui.settingitemview
tsh:title= "Set the display number attribution place"
tsh:desc_on= "Setting the display number attribution place has been opened"
tsh:desc_off= "Set display number attribution has been 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, we customize the control, set the state
Invokes the Setonclicklistener () method of the Settingitemview object, setting the Click event, overriding the OnClick method
Invokes the ischecked () method of the Settingitemview object to get whether the currently selected
Judge state, call the Settingitemview object's setchecked () method, set state, Parameter: Boolean
Call the StartService () method to open the service that listens on the phone's status, parameters: Intent object,
Call the StopService () method to turn off the service
Determines whether the current service is turned on and sets the default check state of the control
Create a new tool class Servicesutils.java
Defines a static method Isservicerunning (), passing in Parameters: Context contexts, string service names
Invokes the Getsystemservice () method of the context object to get the Activitymanager object, parameters: Context.activity_service
Call the Activitymanager object's Getrunningservices () method to get the running Service list collection, parameters: int maximum
For loop list collection, each one is a Runningserviceinfo object
Call RunningServiceInfo.servie.getClassName (), Get to String service class name, and judge if equality returns true
Settingactivity.java
Package Com.qingguow.mobilesafe;
Import android.app.Activity;
Import android.content.Intent;
Import android.content.SharedPreferences;
Import Android.content.SharedPreferences.Editor;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Com.qingguow.mobilesafe.service.AddressService;
Import Com.qingguow.mobilesafe.ui.SettingItemView;
Import Com.qingguow.mobilesafe.utils.ServiceUtils;
public class Settingactivity extends activity {private Settingitemview siv_item;
Private Sharedpreferences sp;
Set whether to open the number belonging to private Settingitemview showaddressbtn;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_setting);
Set Number Attribution Place showaddressbtn = (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 (New Onclicklistener () {@Override public void OnClick (View arg0) {
if (showaddressbtn.ischecked ()) {showaddressbtn.setchecked (false);
StopService (New Intent (Getapplicationcontext (), addressservice.class));
else {showaddressbtn.setchecked (true);
StartService (New Intent (Getapplicationcontext (), addressservice.class));
}
}
});
Siv_item = (Settingitemview) Findviewbyid (R.id.siv_item);
SP = getsharedpreferences ("config", mode_private);
Set state Boolean update = Sp.getboolean ("Update", false) based on the saved data
if (update) {siv_item.setchecked (true);
else {siv_item.setchecked (false); //Automatically updated click event Siv_item.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View arg0) {Editor Editor = Sp.edit();
if (siv_item.ischecked ()) {//setting does not select Siv_item.setchecked (false);
Editor.putboolean ("Update", false);
else {//Set check siv_item.setchecked (true);
Editor.putboolean ("Update", true);
} editor.commit ();
}
});
}
}
Servicesutils.java
Package com.qingguow.mobilesafe.utils;
Import java.util.List;
Import Android.app.ActivityManager;
Import Android.app.ActivityManager.RunningServiceInfo;
Import Android.content.Context;
/** *
Service Tool class
* @author Taoshihan
* * */public
class Serviceutils {
/** * to
determine whether a service is open
* @param context
* @param serviceName
* @return
/public static Boolean Isrunningservice Context,string serviceName) {
Activitymanager am= (Activitymanager) Context.getsystemservice (context.activity_ SERVICE);
List<runningserviceinfo> infos=am.getrunningservices (MB);
for (Runningserviceinfo Info:infos) {
String name=info.service.getclassname ();
if (Name.equals (ServiceName)) {return
true;
}
}
return false;
}
}
The setting effect is as follows:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.