Android Phone Manager (Telephonymanger) Example:
Telephonymanger is a service class that manages phone status and network information.
To add a permission:
<uses-permission
android:name= "Android.permission.READ_PHONE_STATE"/>
<uses-permission
Android:name= "Android.permission.ACCESS_COARSE_LOCATION"/>
Logical function:
Package com.example.telephonystatus;
Import java.io.FileNotFoundException;
Import Java.io.OutputStream;
Import Java.io.PrintStream;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.content.Context;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.TelephonyManager;
Import Android.view.Menu;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
public class Mainactivity extends activity {private ListView list1;
The declaration represents an array of state names private string[] statusname;
Statement represents the collection of mobile status names private arraylist<string> statusvalues = new arraylist<string> ();
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
List1 = (ListView) Findviewbyid (R.ID.LIST1); Get the Telepho of the systemNymanager Telephonymanager Tele = (Telephonymanager) getsystemservice (Context.telephony_service);
Gets an array of various state names Statusname = Getresources (). Getstringarray (R.array.statusnames);
Gets an array of the status of the SIM card string[] Simtype = Getresources (). Getstringarray (R.array.simstate);
Gets an array of phone network types string[] Phonetype = Getresources (). Getstringarray (R.array.phonetype);
Get the device number Statusvalues.add (Tele.getdeviceid ()); Gets the version statusvalues.add of the system platform (tele.getdevicesoftwareversion ()!= null tele. Getdevicesoftwareversion (): "
Unknown ");
Get Network Operation code Statusvalues.add (Tele.getnetworkoperator ());
Get the name of the network operator Statusvalues.add (Tele.getnetworkoperatorname ());
Get the type of mobile phone network Statusvalues.add (Phonetype[tele.getphonetype ()));
Gets the location that is set Statusvalues.add (tele.getcelllocation ()!= null? Tele. Getcelllocation (). toString (): "Unknown");
Obtain the SIM card's Country Statusvalues.add (Tele.getsimcountryiso ());
Get the serial number of the SIM card Statusvalues.add (Tele.getsimserialnumber ());
Gets the status Statusvalues.add (Simtype[tele.getsimstate ()]) of the SIM card;
list<map<string, object>> list = new arraylist<map<string, object>> (); for (int i = 0; i < statusvalues.size (); i++) {hashmap<string, object> hasp = new hashmap<string, Obje
Ct> ();
Hasp.put ("name", Statusname[i]);
Hasp.put ("Values", Statusvalues.get (i));
List.add (HASP); Simpleadapter simple = new Simpleadapter (this, list, R.layout.items, new string[] {"Name", "Values"}, NE
W int[] {r.id.text1, r.id.text2});
List1.setadapter (simple); Create a phone listener phonestatelistener listener = new Phonestatelistener () {//Monitor phone call status @Overri
De public void oncallstatechanged (int state, String incomingnumber) {switch (state) {
No State case Telephonymanager.call_state_idle: Break
Case TelephonyManager.CALL_STATE_OFFHOOK:break;
Caller Bell case TelephonyManager.CALL_STATE_RINGING:OutputStream OS = null;
try {os = openfileoutput ("Phonelist", mode_append);
catch (FileNotFoundException e) {e.printstacktrace ();
} printstream PS = new PrintStream (OS);
The phone number is recorded in the file Ps.println (new Date () + "Caller:" + incomingnumber);
Ps.close ();
Break
Default:break;
} super.oncallstatechanged (state, Incomingnumber);
}
};
Tele.listen (Listener, phonestatelistener.listen_call_state); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio
n Bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
Android SMS Manager (Smsmanager) example
Permissions to register:
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= " Android.permission.SEND_SMS "/>
Mass Message function:
Package com.android.xiong.groupsend;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import android.app.PendingIntent;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.database.Cursor;
Import Android.os.Bundle;
Import Android.provider.ContactsContract;
Import Android.telephony.SmsManager;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.View.OnClickListener;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.CheckBox;
Import Android.widget.EditText;
Import Android.widget.ListView;
Import Android.widget.Toast;
public class Mainactivity extends activity {private Button bt1, BT2;
Private EditText ed1, Ed2;
Private Smsmanager Smanger;
list<string> sendlist = new arraylist<string> (); @Override protected void OnCreate (Bundle savedinstancestaTE) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
BT1 = (Button) Findviewbyid (R.ID.BT1);
BT2 = (Button) Findviewbyid (R.ID.BT2);
ed1 = (edittext) Findviewbyid (r.id.ed1);
Ed2 = (edittext) Findviewbyid (R.ID.ED2);
Get Smsmanger Smanger = Smsmanager.getdefault (); Bt1.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {for (Strin
G send:sendlist) {//Create Pendintent object Pendingintent ped = Pendingintent.getactivity (
Mainactivity.this, 0, New Intent (), 0);
Send Message smanger.sendtextmessage (send, NULL, Ed2.gettext (). ToString (), PED, NULL);
//Prompts the message to send over Toast.maketext (mainactivity.this, "Mass sms End", Toast.length_long). Show ();
}
});
Bt2.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { View the contact's phone number final Cursor Cursor = Getcontentresolver (). Query (CONTACTSCONTRACT.COMMONDATAKINDS.P Hone.
Content_uri, NULL, NULL, NULL, NULL); Baseadapter adapter = new Baseadapter () {@Override public view getview (int position, View convert
View, ViewGroup parent) {cursor.movetoposition (position);
CheckBox RB = new checkbox (mainactivity.this); Gets the phone number of the contact and removes the middle drawing, the space String numbers = cursor. getString (cursor.get
ColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER)). Replace ("-", "");
Rb.settext (number);
If the number has been added to the sender list, the number if (sendlist.contains) {rb.setchecked (True) is checked by default;
return RB; @Override public long getitemid (int position) {//TODO auto-generated Method stub return position;
@Override public Object getitem (int position) {//TODO auto-generated method stub
return position; @Override public int GetCount () {//TODO auto-generated method stub R
Eturn Cursor.getcount ();
}
};
Load the List.xml layout file for the view view Selectview = Getlayoutinflater (). Inflate (R.layout.item, NULL);
Final ListView ListView = (ListView) selectview. Findviewbyid (R.id.list1);
Listview.setadapter (adapter); New Alertdialog.builder (Mainactivity.this). Setview (Selectview). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog
, int which) {//Empty sendlist set Sendlist.clear (); Iterate through the ListView component for each list item for (intI=0;i<listview.getcount (); i++) {checkbox checkbox= (checkbox) Listview.getchildat (i); If the list item is checked if (checkbox.ischecked ()) {//Added to the list item Sendlist.add (checkbox.ge
Ttext (). toString ());
Ed1.append (Checkbox.gettext (). toString () + ",");
"}}}). Show ();
}
}); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio
n Bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_parent" "Android:orientation=" vertical "tools:context=". Mainactivity "> <edittext android:id=" @+id/ed1 "android:layout_width=" Match_parent "Android:l"
ayout_height= "Wrap_content"/> <edittext android:id= "@+id/ed2" android:layout_width= "Match_parent" android:layout_height= "Wrap_content"/> <button android:id= "@+id/bt2" android:layout_width= H_parent "android:layout_height=" wrap_content "android:text=" Get Contact "/> <button android:id=" @+id/bt 1 "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:text=" Send Information "/> < /linearlayout>
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
<listview
android:id= "@+id/list1" android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
</ListView>
</LinearLayout>