Android应用开发揭秘的第3个程序3_2_Content_Provider修改版和高仿版的源码注释
2012-11-05 07:55
447 查看
//读取电话本联系人名字和号码.
Activity01.java
package com.Content_Provider;
import android.app.Activity;
import android.content.ContentResolver; //为正确使用ContentResolver cr
import android.database.Cursor; //为正确使用Cursor cursor
import android.os.Bundle;
import android.provider.ContactsContract; //为正确使用ContactsContract.Contacts.CONTENT_URI
import android.provider.ContactsContract.PhoneLookup; //为正确使用PhoneLookup.DISPLAY_NAME
import android.widget.TextView; //为正确使用TextView tv
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
TextView tv = new TextView(this);
String string = "";
super.onCreate(savedInstanceState);
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//向下移动一下光标
while(cursor.moveToNext())
{
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
//int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
//String number = "123";//cursor.getString(numberFieldColumnIndex);
//取得电话号码
String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
while(phone.moveToNext())
{
String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
string += (contact + ":" + PhoneNumber + "\n");
}
//string += (contact+":"+number+"\n");
}
cursor.close();
//设置TextView显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}
}
Gf_Content_ProviderActivity.java
//AndroidManifest.xml加上相应的访问权限
//<uses-permission
//android:name="android.permission.READ_CONTACTS">
// </uses-permission>
package packagename.Gf_Content_Provider;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.TextView;
public class Gf_Content_ProviderActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView tv = new TextView(this);
String string = "";
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//向下移动一下光标
while(cursor.moveToNext())
{
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
//int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
//String number = "123";//cursor.getString(numberFieldColumnIndex);
//取得电话号码
String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
while(phone.moveToNext())
{
String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
string += (contact + ":" + PhoneNumber + "\n");
}
//string += (contact+":"+number+"\n");
}
cursor.close();
//设置TextView显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}
}
Activity01.java
package com.Content_Provider;
import android.app.Activity;
import android.content.ContentResolver; //为正确使用ContentResolver cr
import android.database.Cursor; //为正确使用Cursor cursor
import android.os.Bundle;
import android.provider.ContactsContract; //为正确使用ContactsContract.Contacts.CONTENT_URI
import android.provider.ContactsContract.PhoneLookup; //为正确使用PhoneLookup.DISPLAY_NAME
import android.widget.TextView; //为正确使用TextView tv
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
TextView tv = new TextView(this);
String string = "";
super.onCreate(savedInstanceState);
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//向下移动一下光标
while(cursor.moveToNext())
{
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
//int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
//String number = "123";//cursor.getString(numberFieldColumnIndex);
//取得电话号码
String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
while(phone.moveToNext())
{
String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
string += (contact + ":" + PhoneNumber + "\n");
}
//string += (contact+":"+number+"\n");
}
cursor.close();
//设置TextView显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}
}
Gf_Content_ProviderActivity.java
//AndroidManifest.xml加上相应的访问权限
//<uses-permission
//android:name="android.permission.READ_CONTACTS">
// </uses-permission>
package packagename.Gf_Content_Provider;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.TextView;
public class Gf_Content_ProviderActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView tv = new TextView(this);
String string = "";
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//向下移动一下光标
while(cursor.moveToNext())
{
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
//int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
//String number = "123";//cursor.getString(numberFieldColumnIndex);
//取得电话号码
String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
while(phone.moveToNext())
{
String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
string += (contact + ":" + PhoneNumber + "\n");
}
//string += (contact+":"+number+"\n");
}
cursor.close();
//设置TextView显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}
}
相关文章推荐
- Android应用开发揭秘的第20,21个程序5_15_tween,5_16_tweenxml修改版和高仿版的源码注释
- Android应用开发揭秘的第26个程序12_2_openglcolor修改版和高仿版的源码注释
- Android应用开发揭秘的第二个程序3_1_Android_Intent修改版和高仿版的源码注释
- Android应用开发揭秘的第10个程序5_5_DrawAllShape修改版和高仿版的源码注释
- Android应用开发揭秘的第一个程序HelloAndroid修改版和高仿版的源码注释
- Android应用开发揭秘的第19个程序5_14_GetDisplay修改版和高仿版的源码注释
- Android应用开发揭秘的第14个程序5_9_MatrixScaleImg修改版和高仿版的源码注释
- Android应用开发揭秘的第25个程序12_1_opengl修改版和高仿版的源码注释
- Android应用开发揭秘的第18个程序5_13_SetFlags修改版和高仿版的源码注释
- Android应用开发揭秘的第13个程序5_8_MatrixRotateImg修改版和高仿版的源码注释
- Android应用开发揭秘的第24个程序5_19_gif修改版和高仿版的源码注释
- Android应用开发揭秘的第9个程序5_4_canvas修改版和高仿版的源码注释
- Android应用开发揭秘的第29个程序12_5_Gfopengllw修改版和高仿版的源码注释
- Android应用开发揭秘的第7个程序5_2_SurfaceViewDrawCircle修改版和高仿版的源码注释
- Android应用开发揭秘的第4个程序3_3_MusicService修改版和高仿版的源码注释
- Android应用开发揭秘的第15个程序5_10_WavePixelImg修改版和高仿版的源码注释
- Android应用开发揭秘的第8个程序5_3_PaintDraw修改版和高仿版的源码注释
- Android应用开发揭秘的第27个程序12_3_GfopenglRotate修改版和高仿版的源码注释
- Android应用开发揭秘的第17个程序5_12_DBuffImg修改版和高仿版的源码注释
- Android应用开发揭秘的第11个程序5_6_DrawText修改版和高仿版的源码注释