创建联系人列表并和联系人打电话应用程序

内容: 这个教程教我们怎么开发一个ListActivity应用, 联系人的列表装载到了基于List的View. 然后可以选择其中之一打电话.
目的: 可以非常容易的学会Intents和ListActivities的使用.

Main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     androidrientation="horizontal">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="Name: "
  10. />
  11. <TextView  
  12. android:id="@+id/row_entry"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. />
  16. </LinearLayout>        
复制代码

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.         package="zyf.CallME"
  4.         android:versionCode="1"
  5.         android:versionName="1.0">
  6.         <application
  7.         android:label="@string/app_name"
  8.         android:icon="@drawable/android">
  9.                 <activity
  10.             android:name=".CallME"
  11.             android:label="@string/app_name">
  12.                         <intent-filter>
  13.                                 <action android:name="android.intent.action.MAIN" />
  14.                                 <category android:name=
  15.                                   "android.intent.category.LAUNCHER" />
  16.                         </intent-filter>
  17.                 </activity>
  18.         </application>
  19.         <uses-sdk android:minSdkVersion="2" />
  20. <uses-permission android:name="android.permission.READ_CONTACTS">
  21. </uses-permission>
  22. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  23. </manifest>         
复制代码

Java

  1. package zyf.CallME;
  2. import android.app.ListActivity;
  3. import android.content.*;
  4. import android.database.Cursor;
  5. import android.os.Bundle;
  6. import android.provider.Contacts.People;
  7. import android.view.View;
  8. import android.widget.*;
  9. public class CallME extends ListActivity {
  10.         private String[] name;
  11.         private int[] to;
  12.         private SimpleCursorAdapter sadapter;
  13.         private Intent callIntent;
  14.         private long PhoneID;
  15.         @Override
  16.         public void onCreate(Bundle icicle) {
  17.                 super.onCreate(icicle);
  18.                 try {
  19.                         Cursor c =
  20.                 getContentResolver().query(People.CONTENT_URI, null,
  21.                                         null, null, null);
  22.                         startManagingCursor(c);
  23.                         name = new String[] { People.NAME };
  24.                         to = new int[] { R.id.row_entry };
  25.                         sadapter = new SimpleCursorAdapter(this,R.layout.main, c, name, to);
  26.                         this.setListAdapter(sadapter);
  27.                 } catch (Exception e) {
  28.                         Toast.makeText(this, "联系人读取错误",
  29.                                       Toast.LENGTH_LONG).show();
  30.                 }
  31.         }
  32.         @Override
  33.         protected void onListItemClick(ListView l,
  34.                                        View v, int position, long id) {
  35.                 super.onListItemClick(l, v, position, id);
  36.                 callIntent=new Intent(Intent.ACTION_CALL);
  37.                 Cursor c=(Cursor)sadapter.getItem(position);
  38.                 PhoneID=c.getLong(c.getColumnIndex(People.PRIMARY_PHONE_ID));
  39.                 callIntent.setData(ContentUris.withAppendedId(android.provider.
  40.                                              Contacts.Phones.CONTENT_URI, PhoneID));
  41.                 startActivity(callIntent);
  42.         }
  43. }        
复制代码

解析
1.在main.xml中添加两个TextView
用于显示"Name:"的静态标签

  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="Name: "
  5. />         
复制代码

用于动态添加并显示联系人的姓名内容的TextView

  1. <TextView  
  2. android:id="@+id/row_entry"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. />        
复制代码

2.修改LinearLayout的布局方向

  1. androidrientation="horizontal"  //说明横向排布        
复制代码

3.AndroidManifest.xml中说明使用权限

  1. <uses-permission
  2.         android:name="android.permission.READ_CONTACTS"></uses-permission>
  3. //说明可以访问读取联系人信息
  4. <uses-permission
  5.         android:name="android.permission.CALL_PHONE"></uses-permission>
  6. //说明可以使用打电话功能        
复制代码

4.Java代码中的读取联系人信息处理

  1. try {
  2.          Cursor c =
  3.      getContentResolver().query(People.CONTENT_URI, null,null, null, null);
  4.      //Cursor,该接口提供从数据库提取信息返回结果中随机读写访问
  5.      //ContentResolver 提供应用程序访问Content模型
  6.      //ContentResolver.query()抽取给定的URI,返回一个在结果之上的Cursor
  7.          startManagingCursor(c);
  8.      //该方法允许Activity基于Activity的生命周期来为你处理管理给定的Cursor的生命周期
  9.          name = new String[] { People.NAME };
  10.      //新建用户名数组  People类用于列出联系人 People.NAME得到联系人姓名
  11.          to = new int[] { R.id.row_entry };
  12.      //创建一个TextView引用数组 用来放置获取的 People.NAME
  13.          sadapter = new SimpleCursorAdapter(this,R.layout.main, c, name, to);
  14.                         this.setListAdapter(sadapter);
  15.      //创建一个简单的适配器从一个cursor指针到TextView或是ImageView的map专栏适配器。
  16.      //构造方法(Context,layout,Cursor,from,to),第一参数是设备上下文,第二个参数是布局文件,第三个参数是指向联系人URI的Cursor指针,form代表来源的字符串(联系人名),to把联系人名放到的地方(TextView)
  17. } catch (Exception e) {
  18.                         Toast.makeText(this, "联系人读取错误",Toast.LENGTH_LONG).show();
  19.             //Toast显示提示,不打扰用户。.show()用来显示Toast
  20. }        
复制代码

5.Java代码中的点击事件处理(选择后电话联系该联系人)

  1. protected void onListItemClick(ListView l,View v, int position, long id) {
  2.     // TODO Auto-generated method stub
  3.         super.onListItemClick(l, v, position, id);
  4.         callIntent=new Intent(Intent.ACTION_CALL);
  5.     //创建一个带有Call动作的Intent
  6.         Cursor c=(Cursor)sadapter.getItem(position);
  7.     //通过选中的Items位置来获取相应的联系人指针 Cursor
  8.         PhoneID=c.getLong(c.getColumnIndex(People.PRIMARY_PHONE_ID));
  9.         //获取相应联系人电话号码
  10.         //getLong()返回请求数据的一个长整型
  11.         //为给定的列,返回基于0的索引值
  12.         //People.PRIMARY_PHONE_ID 获取主键电话号码
  13.     callIntent.setData(ContentUris.withAppendedId(android.provider.
  14.                                               Contacts.Phones.CONTENT_URI, PhoneID));
  15.         //为Intent设置操作的数据
  16.         //ContentUris操作带有数据内容的Uri的实用方法、它们带有"Content"体制
  17.         //withAppendedId()把给定的ID添加到path后面 第一个参数是开始的,后面参数是添加的
  18.         startActivity(callIntent);
  19.     //开启Intent
  20. }        
复制代码

结果
图片1.png

图片2.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值