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.