使用指定的Action,Category调用系统Activity
来源:互联网 发布:网络编码 维基百科 编辑:程序博客网 时间:2024/05/21 06:22
Android提供了大量的标准Action,Category:
例子,查看并获取联系人电话
用户点击按钮值会显示系统的联系人列表,当用户单击联系人之后,程序将会显示该联系人的名字和电话
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(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//创建intentIntent 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中 @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.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); }}
0 0
- 使用指定的Action,Category调用系统Activity
- 指定Action、Category调用系统Activity
- 指定Action,Category调用系统Activity
- Android_指定Action和Category调用系统Activity
- 指定Action、Category调用系统Activty
- 指定Action、Category调用系统Activity实例-点击按钮,返回Home桌面
- Android中指定Action、Category调用系统Activity
- 第五篇 使用category action data启动特定的Activity
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- 使用Action、Data属性启动系统Activity
- 使用Action,Data属性启动系统Activity
- 使用Action、Data属性启动系统Activity
- 我的Android进阶之旅------>启动Activity的标准Action和标准Category
- Intent的Action、Category属性
- Android中调用指定包的Activity
- android.intent action.MAIN category.LAUNCHER anroid启动时运行的Activity
- hdu 3016 dp+线段树
- java中的二维数组
- 关于 XML 头声明和standalone 的解释
- 集合补充
- php中使用session避免在刷新页面造成反复提交表单
- 使用指定的Action,Category调用系统Activity
- iOS简单定位
- 学习OpenCV2——访问Mat图像的像素值
- C++之继承
- Hibernate之SchemaExport+配置文件生成表结构
- 集合 作业
- 微信摇一摇里没有周边
- 集合补充
- Vijos P1484-ISBN号码