編輯:關於Android編程
我的風格,廢話不多說了,直接給大家貼代碼了。
具體代碼如下所示:
package com.org.demo.demo; import com.org.wangfeng.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class GetPhoneActivity extends Activity { private EditText editText; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.getphoneactivity); editText = (EditText) findViewById(R.id.et_getphone); button = (Button) findViewById(R.id.bb_getphone); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(GetPhoneActivity.this, ContactActivity.class); startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // System.out.println("resultCode:" + resultCode); // System.out.println("requestCode:" + requestCode); Log.d("jiejie", "被調用了"); if (resultCode == Activity.RESULT_OK) { String phone = data.getStringExtra("phone"); Log.d("jiejie", "_______________"+phone); phone = phone.replaceAll("-", "").replaceAll(" ", "");// 替換-和空格 editText.setText(phone);// 把電話號碼設置給輸入框 } super.onActivityResult(requestCode, resultCode, data); } } package com.org.demo.demo; import java.util.ArrayList; import java.util.HashMap; import com.org.wangfeng.R; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.AdapterView.OnItemClickListener; import android.widget.TextView; public class ContactActivity extends Activity { private ListView lvList; private ArrayList<HashMap<String, String>> readContact; private TextView back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); back =(TextView)findViewById(R.id.back); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); lvList = (ListView) findViewById(R.id.lv_list); readContact = readContact(); // System.out.println(readContact); lvList.setAdapter(new SimpleAdapter(this, readContact, R.layout.contact_list_item, new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone })); lvList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String phone = readContact.get(position).get("phone");// 讀取當前item的電話號碼 Intent intent = new Intent(); intent.putExtra("phone", phone); setResult(Activity.RESULT_OK, intent);// 將數據放在intent中返回給上一個頁面 finish(); } }); } private ArrayList<HashMap<String, String>> readContact() { // 首先,從raw_contacts中讀取聯系人的id("contact_id") // 其次, 根據contact_id從data表中查詢出相應的電話號碼和聯系人名稱 // 然後,根據mimetype來區分哪個是聯系人,哪個是電話號碼 Uri rawContactsUri = Uri .parse("content://com.android.contacts/raw_contacts"); Uri dataUri = Uri.parse("content://com.android.contacts/data"); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); // 從raw_contacts中讀取聯系人的id("contact_id") Cursor rawContactsCursor = getContentResolver().query(rawContactsUri, new String[] { "contact_id" }, null, null, null); if (rawContactsCursor != null) { while (rawContactsCursor.moveToNext()) { String contactId = rawContactsCursor.getString(0); // System.out.println(contactId); // 根據contact_id從data表中查詢出相應的電話號碼和聯系人名稱, 實際上查詢的是視圖view_data Cursor dataCursor = getContentResolver().query(dataUri, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contactId }, null); if (dataCursor != null) { HashMap<String, String> map = new HashMap<String, String>(); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); // System.out.println(contactId + ";" + data1 + ";" // + mimetype); if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { map.put("phone", data1); } else if ("vnd.android.cursor.item/name" .equals(mimetype)) { map.put("name", data1); } } list.add(map); dataCursor.close(); } } rawContactsCursor.close(); } return list; } }
本段代碼到此結束,代碼比較簡單,並附有注釋,希望對大家學習android獲取手機聯系人相關知識有所幫助。
一、技術准備今天我們來看一下如何修改Android中編譯時的資源Id的值,在講解這內容之前,我們需要先了解一下Android中的資源編譯之後的結構和編譯過程,這裡就不多說
在一些Android應用的開發中,需要通過JNI和Android NDK工具實現JAVA和C/C++之間的相互調用。Java Native Interface (JNI)
Service(服務)是一個應用程序組件,可以在後台執行長時間運行的操作,不提供用戶界面。其他應用程序組件可以啟動一個Serivce,它將繼續在後台運行,即使用戶切換到另
本文實例講述了Android開發中MotionEvent坐標獲取方法。分享給大家供大家參考,具體如下:Android MotionEvent中getX()與getRawX