This article is an example of how Android monitors phone status and sends email notification calls. Share to everyone for your reference, specific as follows:
You can use Phonestatelistener to listen to mobile phone status (e.g. standby, call, Bell, etc.) on Android. This example is through it to monitor mobile phone status, when the phone calls, through the mail will send the caller number to the user's mailbox example. The specific procedures are as follows:
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.TelephonyManager;
Import Android.widget.TextView; public class A07activity extends activity {Private TextView tv;//used to display phone status private String emailreceiver= "16*****85@qq.co M "; e-mail address private String emailsubject= "You have caller information, please check!" "; As the message subject/** called when the "the activity is" is a-created. */@SuppressWarnings ("static-access") @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (
Savedinstancestate);
Setcontentview (R.layout.main);
tv= (TextView) Findviewbyid (r.id.tv);
Phonecalllistener pcl=new Phonecalllistener ();
Telephonymanager tm= (Telephonymanager) Getsystemservice (Telephony_service); Tm.listen (PCL, PCL.
Listen_call_state); public class Phonecalllistener extends phonestatelistener{public void oncallstatechanged (int state,string incoming Number) {//need to rewrite the Oncallstatechanged method SwitCH (state) {case TelephonyManager.CALL_STATE_IDLE:tv.setText ("Call_state_idle");
Break
Case TelephonyManager.CALL_STATE_OFFHOOK:tv.setText ("Call_state_offhook");
Break Case TelephonyManager.CALL_STATE_RINGING:tv.setText ("Caller ID" +incomingnumber);
If someone calls, it will automatically send mail to the mailbox to notify the user caller ID//Set up incoming calls when sending mail Intent i=new Intent (Android.content.Intent.ACTION_SEND);
I.settype ("Plain/text");
I.putextra (Android.content.Intent.EXTRA_EMAIL, New string[]{emailreceiver.tostring ());
I.putextra (Android.content.Intent.EXTRA_SUBJECT, emailsubject.tostring ());
I.putextra (Android.content.Intent.EXTRA_TEXT, "telephone call" +incomingnumber);
StartActivity (Intent.createchooser (i, "Caller Information"));
Break
Default:break;
} super.oncallstatechanged (state, Incomingnumber);
}
}
}
You also need to add several appropriate permissions in Androidmanifest.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.my.a07 "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkve
Rsion= "/>" <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" > <activity android:name= ". A07activity "android:label=" @string/app_name "> <intent-filter> <action android:name=" Andr Oid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent -filter> </activity> </application> <uses-permission android:name= "Android.permission.SEND_SMS" & gt;</uses-permission> <uses-permission android:name= "Android.permission.READ_CONTACTS" ></ uses-permission> <uses-permission android:name= "Android.permission.READ_PHONE_STATE" ></
Uses-permission> </manifest>
More interested readers of Android-related content can view this site: "The summary of Android controls usage" and "Android Development introduction and Advanced Course"
I hope this article will help you with the Android program.