根据姓名查询联系⼈号码
最近在学习关于联系⼈的使⽤,要做⼀个关于根据姓查询联系⼈的简单应⽤。在进⾏开发时参考的的是⼀本⽐较“古⽼的”android资料,结果发现再运⾏时报错。经过⼀系列查资料的努⼒后,终于弄清了问题所在。
⾃从android2.0开始,联系⼈API做了很⼤的改动,这些之前我并不清楚,但是在经历这件事之后我才弄明⽩。
下⾯就是具体代码:
BundleActivity:
package com.android.ba;
import android.app.Activity;
t.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class BundleActivity extends Activity
{
private Button mybutton;
private EditText myedittext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.main);
mybutton=(Button) findViewById(R.id.button01);
myedittext=(EditText) findViewById(R.id.edittext01);
mybutton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//获得姓名字符串
String Text().toString();
//实例化Bundle对象
Bundle data=new Bundle();
//添加信息
data.putString("name", name);
//实例化Intent
Intent intent=new Intent(BundleActivity.this, secondActivity.class);
intent.putExtras(data);
姓名代码转换器百度startActivity(intent);
}
});
}
}
secondActivity:
package com.android.ba;
import android.app.Activity;
t.Intent;
import android.database.Cursor;
import android.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
public class secondActivity extends Activity
{
private TextView textview;
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.show);
textview=(TextView) findViewById(view02);
Intent intent=getIntent();
Bundle Extras();
String String("name");
/
/查询返回的项⽬
String[] projection=new String[] {
ContactsContract.Contacts._ID,ContactsContract.PhoneLookup.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};          //设置需要返回的索引资源(Sqlite数据库)
Uri contacts=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
//需要查询的姓名
String[] args={name};
Cursor managedCursor=managedQuery(contacts,
projection,
ContactsContract.PhoneLookup.DISPLAY_NAME+"=?",
args,
ContactsContract.PhoneLookup.DISPLAY_NAME);
veToFirst())
{
String String(1);
String String(2);
textview.setText(name1+":"+number);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="schemas.android/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview01"
android:text="请输⼊姓名"
>
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edittext01"
android:text=""
>
</EditText>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button01"
android:text="查询"
>
</Button>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview02"
android:text="">
</TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="schemas.android/apk/res/android" package="com.android.ba"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".BundleActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="secondActivity"></activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>    <uses-permission android:name="android.permission.WRITE_CONTACTS"/> </manifest>
下⾯是managedQuery的详细解析:。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。