編輯:關於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); } }
[android] 天氣app布局練習,androidapp主要練習一下RelativeLayout和LinearLayout <RelativeLayout
Android項目實戰(二十九):酒店預定日期選擇,android酒店預定先看需求效果圖: 幾個需求點: 1、顯示
判斷App是否在後台運行,判斷App後台運行 在一些場景中,經常會需要判斷App是否在後台運行,比如是否顯示解鎖界面,收到新消息是否顯示Notificatio
[android] 手機衛士手機實現短信指令獲取位置,android衛士獲取位置 新建一個service的包 新建一個GPSService類繼承系統的Serv