Android monitoring incoming call display

Source: Internet
Author: User

When we call a phone, we need to monitor the incoming call of the phone number.CodeControl. Let's take a look at the display of incoming calls using the display feature.
 


Package com. Smart;

Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. telephony. phonestatelistener;
Import Android. telephony. telephonymanager;
Import Android. widget. Toast;

Public class main extends activity {

Public class myphonecalllistener extends phonestatelistener {

@ Override
Public void oncallstatechanged (INT state, string incomingnumber ){

Switch (state ){
Case telephonymanager. call_state_offhook:
Toast. maketext (main. This, "In call...", Toast. length_short). Show ();
Break;


Case telephonymanager. call_state_ringing:
Toast. maketext (main. This, incomingnumber, Toast. length_short). Show ();
Break;
}




Super. oncallstatechanged (State, incomingnumber );
}

}

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Telephonymanager TM = (telephonymanager) getsystemservice (context. telephony_service );
Myphonecalllistener = new myphonecalllistener ();
TM. Listen (myphonecalllistener, phonestatelistener. listen_call_state );

}
}

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>


<Textview
Android: Id = "@ + ID/phonestate"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"

/>
</Linearlayout>

 

 

Main. Xml

<? XML
Version = "1.0" encoding = "UTF-8"?>
< Manifest
Xmlns: Android = "http://schemas.android.com/apk/res/android
"
Package = "org. Me. android_callstate">
< Application
>
< Activity
Android: Name = ". mainactivity" Android: Label = "mainactivity">
< Intent-Filter
>
< Action
Android: Name = "android. Intent. Action. Main"/>
< Category
Android: Name = "android. Intent. Category. launcher"/>
</ Intent-Filter
>
</ Activity
>
</Application
>
<
Uses-Permission Android: Name = "android. Permission. read_phone_state"> </
Uses-Permission>


</ Manifest
>

 

Do not forget the red font above, and do not make any mistakes.

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.