Today, let's talk about how to intercept the call log in Android.
In Android, like text messages, there is also a set of message (broadcast) mechanisms. We only need to register a receiver to receive broadcast related to Android phones, the following code is a complete broadcasreceiver class that can listen to incoming and outgoing calls in the Android system:
Package com. Contact. Main. Cycler;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. OS. ibinder;
Import Android. telephony. telephonymanager;
Import Android. util. log;
Public class incommingcalllogreceiver extends broadcastreceiver {
Private final string tag = incommingcalllogpolicer. Class. getsimplename ();
Public static final string action_phone_state = telephonymanager. action_phone_state_changed;
Private context mcontext;
@ Override
Public ibinder peekservice (final context mycontext, final intent Service ){
Return super. peekservice (mycontext, Service );
}
@ Override
Public void onreceive (context, intent ){
Mcontext = context;
Final string action = intent. getaction ();
Final string state = intent. getstringextra (telephonymanager. extra_state );
Log. E (TAG, "Call state changed, Action =" + action );
If (action_phone_state.equals (Action) {// call status change
Log. E (TAG, "phone status changed ");
If (telephonymanager. extra_state_ringing.equals (State) {// call ringing
Final string number = intent. getstringextra (telephonymanager. extra_incoming_number );
Log. E (TAG, "Number:" + number );
Log. E (TAG, "the phone is ringing... ");
} Else if (telephonymanager. extra_state_offhook.equals (State) {// call offhook
Log. E (TAG, "Answer ");
} Else if (telephonymanager. extra_state_idle.equals (State) {// call idle
Log. E (TAG, "hanging up ");
}
} Else if (intent. action_new_outgoing_call.equals (Action) {// intercept outgoing calls
Final string phonenumber = intent. getstringextra (intent. extra_phone_number );
Log. E (TAG, "outgoing call, outgoing call number:" + phonenumber );
}
}
}
Of course, to work with this handler, you must register it in androidmanifest. xml:
<Cycler Android: Name = "com. Contact. Main. cycler. incommingcalllogpolicer">
<Intent-filter Android: Priority = "10000">
<Action Android: Name = "android. Intent. Action. phone_state"/>
<Action Android: Name = "android. Intent. Action. new_outgoing_call"/>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
</Intent-filter>
</Cycler>
This is not enough because we still lack the corresponding operation permissions:
<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
<Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/>
<Uses-Permission Android: Name = "android. Permission. Internet"/>
<Uses-Permission Android: Name = "android. Permission. write_settings"/>
<Uses-Permission Android: Name = "android. Permission. process_outgoing_cils">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. Vibrate">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. Internet">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. get_tasks">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. access_network_state">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. read_contacts">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. write_contacts">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. read_sms">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. write_sms">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. write_external_storage">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. call_phone">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. read_phone_state">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. receive_sms">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. send_sms">
</Uses-Permission>
The above permissions are complete, including all permissions for operating Android system contacts, text messages, and call records.
<Uses-Permission Android: Name = "android. Permission. process_outgoing_cils">
</Uses-Permission>
This permission is critical. In the absence of this permission, we cannot monitor the phone number sent from the local machine, or we can monitor the phone number, but we cannot tell whether the phone number is sent or not, with this permission added, we can monitor: intent. action_new_outgoing_call broadcast:
Public static final string action_new_outgoing_call =
"Android. Intent. Action. new_outgoing_call ";
Original address http://www.cppcode.com/archives/2012/04/01/259.html