How to monitor incoming calls in Android and generate a floating form prompt

Source: Internet
Author: User

 

Recently, for project reasons, we need to implement an incoming call listener and generate a floating window to prompt relevant information (which is inconvenient to disclose ).

Now let me give a rough picture of my ideas and implementation methods.

 

To listen for incoming calls, you must first declare "android. Permission. read_phone_state" permission in manifest.

 

XML Code
  1. <Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>

 

You also need to register an incoming call listener. Currently, my solution is to receive the incoming call broadcast and register the incoming call listener after receiving the broadcast. "Android. Permission. receive_boot_completed" permission is required to receive the boot broadcast. The manifest statement is as follows:

 

Java code
  1. <Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>

 

Then register the broadcast receiving Class

 

XML Code
  1. <Cycler Android: Name = ". phonebootreceiver">
  2. <Intent-filter>
  3. <Action Android: Name = "android. Intent. Action. boot_completed"/>
  4. </Intent-filter>
  5. </Cycler>
<Cycler Android: Name = ". phonebootreceiver "> <intent-filter> <action Android: Name =" android. intent. action. boot_completed "/> </intent-filter> </Cycler>

 

Register a listener in phonebootreceiver. First, you must obtain the System Service "telephony_service"

 

Java code
  1. Telephonymanager telm = (telephonymanager) getsystemservice (context. telephony_service );
Telephonymanager telm = (telephonymanager) getsystemservice (context. telephony_service );

 

Then add a listener

 

Java code
  1. Telm. Listen (New tellistener (context), phonestatelistener. listen_call_state );
Telm. Listen (New tellistener (context), phonestatelistener. listen_call_state );

 

Tellistener is a custom phone status listener class inherited from phonestatelistener. To listen to incoming calls, you only need to implement the oncallstatechanged (INT state, string incomingnumber) method.

In the title, the floating window is displayed. In fact, the floating window is added to the windowmanager. This function is also implemented in tellistener. To implement a floating window, you must first have the "android. Permission. system_alert_window" permission and declare it in manifest as follows:

 

XML Code
  1. <Uses-Permission Android: Name = "android. Permission. system_alert_window"/>
<Uses-Permission Android: Name = "android. Permission. system_alert_window"/>

 

Windowmanager needs to use context. getapplicationcontext (). getsystemservice (context. window_service );

.

 

First, release the tellistener source code, and then explain it in detail.

 

XML Code
  1. Public class tellistener extends phonestatelistener {
  2. Private context;
  3. Private windowmanager WM;
  4. Private textview TV;
  5. Public tellistener (context ){
  6. This. Context = context;
  7. }
  8. @ Override
  9. Public void oncallstatechanged (INT state, string incomingnumber ){
  10. // Todo auto-generated method stub
  11. Super. oncallstatechanged (State, incomingnumber );
  12. If (State = telephonymanager. call_state_ringing ){
  13. WM = (windowmanager) Context. getapplicationcontext (). getsystemservice (context. window_service );
  14. Windowmanager. layoutparams Params = new windowmanager. layoutparams ();
  15. Params. type = windowmanager. layoutparams. type_system_overlay;
  16. Params. Flags = windowmanager. layoutparams. flag_not_touch_modal | windowmanager. layoutparams. flag_not_focusable;
  17. Params. width = windowmanager. layoutparams. wrap_content;
  18. Params. Height = windowmanager. layoutparams. wrap_content;
  19. <Span style = "white-space: pre"> </span> Params. format = pixelformat. rgba_8888;
  20. TV = new textview (context );
  21. TV. settext ("this is a floating window, call number:" + incomingnumber );
  22. WM. addview (TV, Params );
  23. } Else if (State = telephonymanager. call_state_idle ){
  24. If (WM! = NULL ){
  25. WM. removeview (TV );
  26. }
  27. }
  28. }
  29. }
Public class tellistener extends phonestatelistener {private context; private windowmanager WM; private textview TV; Public tellistener (context) {This. context = context;} @ overridepublic void oncallstatechanged (INT state, string incomingnumber) {// todo auto-generated method stubsuper. oncallstatechanged (State, incomingnumber); If (State = telephonymanager. call_state_ringing) {WM = (invalid Wm Anager) context. getapplicationcontext (). getsystemservice (context. window_service); windowmanager. layoutparams Params = new windowmanager. layoutparams (); Params. type = windowmanager. layoutparams. type_system_overlay; Params. flags = windowmanager. layoutparams. flag_not_touch_modal | windowmanager. layoutparams. flag_not_focusable; Params. width = windowmanager. layoutparams. wrap_content; Params. height = Win Dowmanager. layoutparams. wrap_content; Params. format = pixelformat. rgba_8888; TV = new textview (context); TV. settext ("this is a floating window, call number:" + incomingnumber); WM. addview (TV, Params);} else if (State = telephonymanager. call_state_idle) {If (WM! = NULL) {WM. removeview (TV );}}}}

 

State = telephonymanager. call_state_ringing indicates a new call, and state = telephonymanager. call_state_idle indicates that the call is interrupted (the call may not be accurate. The state will be equal to telephonymanager. call_state_idle)

 

Define window layout

 

Java code
  1. Windowmanager. layoutparams Params = new windowmanager. layoutparams ();
Windowmanager. layoutparams Params = new windowmanager. layoutparams ();

 

Set the Window Type above all windows

 

Java code
  1. Params. type = windowmanager. layoutparams. type_system_overlay;
Params. type = windowmanager. layoutparams. type_system_overlay;

 

Don't forget

 

Java code
  1. Params. Flags = windowmanager. layoutparams. flag_not_touch_modal | windowmanager. layoutparams. flag_not_focusable;
Params. Flags = windowmanager. layoutparams. flag_not_touch_modal | windowmanager. layoutparams. flag_not_focusable;

If there is no such sentence, after the floating window is generated, the interface after the floating window cannot be clicked in the east or west. The purpose of this sentence is to make the floating window lose focus.

 

Transparent background

Java code
  1. Params. format = pixelformat. rgba_8888;
Params. format = pixelformat. rgba_8888;

 

 

In this example, the floating window only displays a textview whose content is "this is a floating window, call number: xxxxxx", and finally adds textview to the form.

 

Java code
  1. WM. addview (TV, Params );
WM. addview (TV, Params );

 

Remove textview after the phone is disconnected, otherwise it will be displayed all the time...

 

Java code
  1. WM. removeview (TV );
WM. removeview (TV );

 

This article is here...

"What? To be movable ?"

If you want to drag it, add setontouchlistener to textview to implement the ontouchlistener method of ontouchlistener.

By the way, do not forget

Java code
  1. Params. type = windowmanager. layoutparams. type_system_overlay;
Params. type = windowmanager. layoutparams. type_system_overlay;

Change

Java code
  1. Params. type = windowmanager. layoutparams. type_phone;
Params. type = windowmanager. layoutparams. type_phone;

Because type_system_overlay cannot obtain the input focus of textview, it cannot be dragged.

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.