Android 2.2 obtains contacts, calls, and dials

Source: Internet
Author: User

This demo is the first Android-based development.

The main functions include reading the contact name and number, displaying the lisetview, obtaining listview data, concurrent text messages, or dialing.

 

Package com. Android. Hello;

Import Android. App. activity;
Import Android. content. intent;
Import Android. database. cursor;
Import Android. Graphics. color;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. telephony. phonenumberutils;
Import Android. util. log;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. linearlayout;
Import Android. widget. listadapter;
Import Android. widget. listview;
Import Android. widget. relativelayout;
Import Android. widget. textview;
Import Android. widget. Toast;
Import Android. provider. contactscontract;

Import java. util. arraylist;
Import java. util. hashmap;
Import Android. widget. simpleadapter;

@ Suppresswarnings ("deprecation ")
Public class Hello extends activity {
/** Called when the activity is first created .*/
// @ Suppresswarnings ("deprecation ")
// @ Override
//
Private Static final string tag = "app ";
Listview;
Listadapter adapter;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Setcontentview (R. layout. Main );
Linearlayout = new linearlayout (this );
Linearlayout. setorientation (linearlayout. Vertical );
Linearlayout. setbackgroundcolor (color. Black );
Linearlayout. layoutparams Param = new linearlayout. layoutparams (linearlayout. layoutparams. fill_parent, linearlayout. layoutparams. wrap_content );

Listview = new listview (this );
Listview. setbackgroundcolor (color. Black );

Linearlayout. addview (listview, Param );

This. setcontentview (linearlayout );


// Generate a dynamic array and add data
Arraylist Arraylist Cursor cursor = getcontentresolver (). Query (contactscontract. Contacts. content_uri, null );
While (cursor. movetonext ())
{
Hashmap <string, Object> map = new hashmap <string, Object> ();
String phonename = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. display_name ));
Map. Put ("itemtitle", phonename); // telephone name
String contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id ));
String hasphone = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. has_phone_number ));

If (hasphone. compareto ("1") = 0)
{
Cursor phones = getcontentresolver (). Query (contactscontract. commondatakinds. Phone. content_uri, null, contactscontract. example. Phone. contact_id + "=" + contactid, null, null );
While (phones. movetonext ())
{
String phonenumber = phones. getstring (phones. getcolumnindex (contactscontract. commondatakinds. Phone. Number ));
String phonetpye = phones. getstring (phones. getcolumnindex (contactscontract. commondatakinds. Phone. Type ));

Map. Put ("itemtext", phonenumber); // how to handle multiple numbers

Log. D (TAG, "testnum =" + phonenumber + "type:" + phonetpye );
}
Phones. Close ();
}
Cursor emails = getcontentresolver (). Query (contactscontract. commondatakinds. Email. content_uri, null, contactscontract. Contents. Email. contact_id + "=" + contactid, null, null );
While (emails. movetonext ())
{
String emailaddress = emails. getstring (emails. getcolumnindex (contactscontract. commondatakinds. Email. Data ));
String emailtype = emails. getstring (emails. getcolumnindex (contactscontract. commondatakinds. Email. Type ));

Log. D (TAG, "testnum =" + emailaddress + "type:" + emailtype );
}
Emails. Close ();

Listitem. Add (MAP );
}

// Generate the element corresponding to the item and dynamic array of the adapter
Simpleadapter listitemadapter = new simpleadapter (this, listitem, // Data Source
Android. R. layout. simple_list_item_2, // XML Implementation of listitem
// Subitem of the dynamic array and imageitem
New String [] {"itemtitle", "itemtext "},
// An imageview in the XML file of imageitem, two textview IDS
New int [] {Android. R. Id. text1, Android. R. Id. text2}
);

Listview. setadapter (listitemadapter );
Cursor. Close ();

// Listview. setemptyview (findviewbyid (R. Id. Empty ));

Listview. setonitemselectedlistener (New adapterview. onitemselectedlistener (){

Public void onitemselected (adapterview <?> Arg0, view arg1,
Int arg2, long arg3 ){
// Todo auto-generated method stub
// Opentoast ("scroll to:" + arg0.getselecteditemid ());
// Send SMS
Settitle ("select" + arg2 + "project ");
Opentoast ("select" + arg0.getselecteditemid () + "project ");
Relativelayout LR = (relativelayout) arg1;
Textview mtext = (textview) LR. getchildat (1 );
Opentoast (mtext. gettext (). tostring ());

String number = mtext. gettext (). tostring ();
Log. D (TAG, "Number =" + number );
// Determine the validity of the phone number
If (phonenumberutils. isglobalphonenumber (number )){
Intent intent = new intent (intent. action_sendto, Uri
. Parse ("smsto: //" + number ));
Intent. putextra ("sms_body", "the SMS text ");
Startactivity (intent );
}
}

Public void onnothingselected (adapterview <?> Arg0 ){
// Todo auto-generated method stub

}

});

Listview. setonitemclicklistener (New adapterview. onitemclicklistener (){

Public void onitemclick (adapterview <?> Arg0, view arg1,
Int position, long arg3 ){
// Todo auto-generated method stub
// Opentoast ("click" + integer. tostring (Position + 1) + "project ");
Relativelayout LR = (relativelayout) arg1;
Textview mtext = (textview) LR. getchildat (1 );
Opentoast (mtext. gettext (). tostring ());

String number = mtext. gettext (). tostring ();
Log. D (TAG, "Number =" + number );
// Determine the validity of the phone number
If (phonenumberutils. isglobalphonenumber (number )){
Intent intent = new intent (intent. action_dial, Uri
. Parse ("Tel: //" + number ));
Startactivity (intent );
}
}
});
}
 
Private void opentoast (string Str ){
Toast. maketext (this, STR, Toast. length_short). Show ();
}
}

 

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.