編輯:關於Android編程
首先是運行結果
由於通訊錄在手機裡是以數據庫貯存的 所以我們可以通過一個方法
context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
當然讀取手機通訊錄需要權限 在adnroidManifest文件中聲明即可
由於我也實現了打電話的功能 所以也要聲明權限
<uses-permission android:name="android.permission.READ_CONTACTS"> <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission></uses-permission>布局文件
activity_main.xml
<relativelayout 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"> <listview android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content"> </listview> </relativelayout>listview的布局文件:item.xml,在這裡我設置的頭像為默認的 當然也可以在手機數據庫中讀取聯系人的icon
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:id="@+id/image" android:layout_width="60dp" android:layout_height="60dp" android:padding="10dp" android:src="@drawable/ic_launcher"> <textview android:id="@+id/name" android:paddingtop="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_torightof="@id/image" android:text="name"> <textview android:id="@+id/number" android:paddingtop="5dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/name" android:layout_torightof="@id/image" android:text="number"> </textview></textview></imageview></relativelayout>
自己封裝一個聯系人信息的類 有兩個變量
package com.example.getphonenumber; public class PhoneInfo { private String name; private String number; public PhoneInfo(String name, String number) { this.name = name; this.number = number; } public String getName() { return name; } public String getNumber() { return number; } }
讀取手機數據庫中的通訊錄
GetPhoneNumberFromMobile.class
package com.example.getphonenumber; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract.CommonDataKinds.Phone; public class GetPhoneNumberFromMobile { private List自定義adapterlist; public List getPhoneNumberFromMobile(Context context) { // TODO Auto-generated constructor stub list = new ArrayList (); Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null); //moveToNext方法返回的是一個boolean類型的數據 while (cursor.moveToNext()) { //讀取通訊錄的姓名 String name = cursor.getString(cursor .getColumnIndex(Phone.DISPLAY_NAME)); //讀取通訊錄的號碼 String number = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); PhoneInfo phoneInfo = new PhoneInfo(name, number); list.add(phoneInfo); } return list; } }
package com.example.getphonenumber; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MyAdapter extends BaseAdapter{ private ListMainActivity中listview加載適配器 並為其添加點擊監聽事件list; private Context context; public MyAdapter(List list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null){ ViewHolder viewHolder=new ViewHolder(); LayoutInflater inflater=LayoutInflater.from(context); convertView=inflater.inflate(R.layout.item, null); viewHolder.name=(TextView) convertView.findViewById(R.id.name); viewHolder.number=(TextView) convertView.findViewById(R.id.number); viewHolder.name.setText(list.get(position).getName()); viewHolder.number.setText(list.get(position).getNumber()); } return convertView; } public class ViewHolder{ TextView name; TextView number; } }
package com.example.getphonenumber; import java.util.ArrayList; import java.util.List; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { private ListView lv; private MyAdapter adapter; private GetPhoneNumberFromMobile getPhoneNumberFromMobile; private Listlist = new ArrayList (); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listView1); getPhoneNumberFromMobile = new GetPhoneNumberFromMobile(); list = getPhoneNumberFromMobile.getPhoneNumberFromMobile(this); adapter = new MyAdapter(list, this); lv.setAdapter(adapter); lv.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub String number = list.get(position).getNumber(); Intent intent = new Intent(); intent.setAction("android.intent.action.CALL"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); } }
最近采用Instruments 來分析整個應用程序的性能.發現很多有意思的點,以及性能優化和一些分析性能消耗的技巧,小結如下。Instruments使用技巧關於Instr
轉帖請注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming),請尊重他人的辛勤勞動成果,謝謝! 隨著移動互聯網的快速發展
在OC中,我們可以通過Category 對已有的類進行擴展,這得益於OC的Runtime機制,讓類可以‘動態’的添加方法以及實現。但是,在Cate
最近由於項目需求,需要完成批量照片上傳,折騰了一段時間,終於完成了,達到了如下效果 package com.qian.pos;