使用指定的Action,Category调用系统Activity
最后更新于:2022-04-01 16:14:54
Android提供了大量的标准Action,Category:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9b12e7d7.jpg)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9b146ae9.jpg)
例子,查看并获取联系人电话
用户点击按钮值会显示系统的联系人列表,当用户单击联系人之后,程序将会显示该联系人的名字和电话
MainActivity.java
~~~
package com.hust.getcontacts;
import android.app.Activity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
final int PICK_CONTACT=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建intent
Intent intent=new Intent();
//设置Intent的Action属性
intent.setAction(Intent.ACTION_GET_CONTENT);
//设置Intent的Type属性
intent.setType("vnd.android.cursor.item/phone");
//启动Activity,并希望获取该Activity的结果
startActivityForResult(intent,PICK_CONTACT);
}
});
}
//重写该方法,处理返回的联系人数据在参数Intent data中
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case (PICK_CONTACT):
if(resultCode==Activity.RESULT_OK){//Activity.RESULT_OK标准Activity结果 -1
//获取返回的联系人数据
Uri contactdata=data.getData();
CursorLoader cursorloader=new CursorLoader(this, contactdata, null, null, null, null);
//查询联系人信息表
Cursor cursor=cursorloader.loadInBackground();
//如果查询到指定的联系人
if(cursor.moveToFirst()){
//获取联系人的Id
String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//获取联系人的名字
String name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber="此联系人暂未输入电话号码";
//根据联系人查询该联系人的详细信息
Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null);
//
if(phones.moveToFirst()){
//取出电话号码
phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
EditText show=(EditText) findViewById(R.id.editText1);
show.setText(name);
EditText phone=(EditText) findViewById(R.id.editText2);
phone.setText(phoneNumber);
}
cursor.close();
}
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
~~~
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9b16153d.jpg)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9b1776aa.jpg)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9b19192d.jpg)