Back to project page sms_modi.
The source code is released under:
GNU General Public License
If you think the Android project sms_modi listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.zhufeng.sms_mod; // w w w . j ava 2 s . c o m import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.PhoneLookup; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class ContactActivity extends Activity { private ListView listView; private ListView listView2; private Button button; /* * 1.??????????? 1.1 ????????????????????????????????????????????????????? 1.2 * ?????????,??????????????????????? 1.3 ??????????????????????? */ public static final String[] COLUMNS = { Contacts._ID, Contacts.DISPLAY_NAME }; private static final String[] COLUMNS2 = { Contacts.DISPLAY_NAME, Contacts._ID }; private static final String[] COLUMNS3 = { Phone.NUMBER }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); // TextView tv = new TextView(this); // tv.setText(getContacts()); listView = (ListView) findViewById(R.id.listview); listView2 = (ListView) findViewById(R.id.listview2); button = (Button) findViewById(R.id.button); button.setText("???????????????????????"); button.setOnClickListener(new MyButton()); @SuppressWarnings("deprecation") Cursor cursor = managedQuery(Contacts.CONTENT_URI, COLUMNS, null, null, null); // ????ID???????---??? int idIndex = cursor.getColumnIndex(COLUMNS[0]); // ????NAME???????---??? int displayNameIndex = cursor.getColumnIndex(COLUMNS[1]); List<String> items = new ArrayList<String>(); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { int id = cursor.getInt(idIndex); String name = cursor.getString(displayNameIndex); items.add("id=" + id + "\t ????= " + name); } /* ArrayAdapter<String> adapter = new ArrayAdapter<String>( * ContactActivity.this, R.layout.list_item, items); * listView.setAdapter(adapter); */ Toast.makeText(this, this.getString(R.string.toast_end), Toast.LENGTH_LONG).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.contact, menu); return true; } public class MyButton implements OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.button: getNameAndPhone(); break; default: break; } } } @SuppressWarnings("deprecation") public void getNameAndPhone() { Cursor cursor = managedQuery(Contacts.CONTENT_URI, COLUMNS2, null, null, null); int displayNameIndex = cursor.getColumnIndex(COLUMNS2[0]); List<String> items = new ArrayList<String>(); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { String name = cursor.getString(displayNameIndex); int id = cursor.getInt(cursor.getColumnIndex(COLUMNS2[1])); Cursor phone = managedQuery(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + id, null, null); while (phone.moveToNext()) { String phoneNumber = phone.getString(phone .getColumnIndex(COLUMNS3[0])); items.add("?????" + name + "\t ???" + phoneNumber); } } /* ArrayAdapter<String> adapter = new ArrayAdapter<String>( * ContactActivity.this, R.layout.list_item, items); * listView2.setAdapter(adapter); */ } // ????????? public String getContacts() { ContentResolver cr = getContentResolver(); Uri URI = ContactsContract.Contacts.CONTENT_URI; String[] columns = new String[] { ContactsContract.Contacts._ID, PhoneLookup.DISPLAY_NAME }; // ???????ID???????????? Cursor cursor = cr.query(URI, columns, PhoneLookup.HAS_PHONE_NUMBER + "=1", null, null); // ???????????????????? while (cursor.moveToNext()) { String phoneNum = ""; Cursor cursor2 = cr .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + cursor.getLong(0), null, null); // ?????????????????????,????????????????????, // ???????????ID???????,??phoneNum? while (cursor2.moveToNext()) { phoneNum += cursor2.getString(0) + " "; // ???????????????????phoneNum } cursor2.close(); System.out.println(cursor.getLong(0) + ":" + cursor.getString(1) + phoneNum); // ????ID,????,????? } cursor.close(); return "Query Contacts Successfully!"; } }