Ideas:
1. Since we are not sure when there will be a call, we should use the service to listen for incoming calls.
2. Cancel the monitoring when the service is closed
3. Set the checkbox status on the page by opening and closing the service.
Listening to incoming calls via services
- Create a class that inherits from the service
- In the OnCreate method, perform a listener operation on the incoming call:
1) Telephonymanager manager = Getsystemservice (telephony_service);
2) Manager.listen (Listener, phonestatelistener.listen_call_state);
Where the first parameter is an Phonestatelistener object.
Second parameter setting we're going to listen to God's horse.
Write an inner class that inherits from Phonestatelistener
Override the Oncallstatechanged method, which will callback when the session changes.
In which to determine whether the status of the call, if the status of the call, get the call number, through the number to obtain the attribution.
Finally, in OnDestroy, the listener service is closed.
Manager.listen (Listener,phonestatlistener.listen_none);
Concrete Implementation code:
public class Callservice extends Service
{
Private Telephonymanager Manager;
Private Myphonestatelistener listener;
Public Callservice () {
}
public IBinder onBind(Intent intent) { return null;}private class MyPhoneStateListener extends PhoneStateListener{ //当电话状态发生变化的时候 , 回调 public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING: String location = QueryNumberUtils.getLocationByNumber(incomingNumber); Toast.makeText(getApplicationContext(),location,Toast.LENGTH_LONG).show(); break; } }}public void onCreate(){ super.onCreate(); //监听来电 manager = (TelephonyManager) getApplicationContext().getSystemService(TELEPHONY_SERVICE); manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);}public void onDestroy(){ super.onDestroy(); manager.listen(listener,PhoneStateListener.LISTEN_NONE); manager = null;}
}
Determine if service is turned on
Create a method:
Serviceisworking (Context context,string serviceName)
{
Get a Manager
Activitymanager am = Context.getsystemservice (context.activity_service);
Get Service Information
List<RunningServiceInfo> infos = am.getRunningServices(100);
Traversal Service
for (Runningserviceinfo Info:infos)
Compare by Info.service.getClassName () and service class name
Returns True if same.
}
Determine if a service is turned on
The final implementation code:
Public Static Boolean serviceisworking(Context context,string ServiceName) {//Create a Component Manager to get to system servicesActivitymanager manager = (Activitymanager) context.getsystemservice (Context.activity_service);//Get information about the service that is runninglist<activitymanager.runningserviceinfo> infos = manager.getrunningservices ( -);//Traversal if(Infos! =NULL) { for(Activitymanager.runningserviceinfo Info:infos) {//Get service nameString name = Info.service.getClassName ();if(servicename.equals (name)) {return true; } } }return false; }
Copyright notice: Just out of the original content of the pot, I hope you have help ~
Mobile Security defender------show the operation of the caller's place of ownership