Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32544
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 85
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-27 11:11
个人简介

everything is up to you

文章分类

全部博文(8)

文章存档

2014年(8)

分类: Android平台

2014-05-08 23:31:01

1:使用startActivityForResult()而不是startActivity()启动另外一个Activity
方法的签名是:startActivityForResult(Intent intent, int requestCode)这个方法有几个重载的方法
注意:
传入的Intent可以是隐式Intent或者是显示Intent。但如果是启动一个你自己的Activity,则最好使用显示Intent
在调用 startActivityForResult()时传入一个请求码(这个请求码用来识别请求,即返回的结果是由那个Activity中返回的),当返回结果时,这个请求码会被传回来
2:重写onActivityResult()这个方法接收返回的结果
方法的签名是:onActivityResult(int requestCode, int resultCode, Intent data)
第一个参数:startActivityForResult()方法执行时传入的请求码(请求码如果小于0,则和startActivity()方法一样,是不返回结果的)
第二个参数:返回操作的结果码,如果操作成功返回RESULT_OK,如果操作失败返回RESULT_CANCELED,例如用户自己回退(按BACK键)或其他原因失败
第三个参数:返回的结果,为了处理这个结果,你必须知道返回的Intent的格式。其中Android平台自带的Apps都有相应的API描述指定的结果数据
例子(摘自Android官网http://developer.android.com/training/basics/intents/result.html):

点击(此处)折叠或打开

  1. package com.example.androidgov;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.provider.ContactsContract.CommonDataKinds.Phone;
  8. import android.view.View;
  9. import android.widget.TextView;

  10. public class ReturnResultActivity extends Activity {

  11.     static final int PICK_CONTACT_REQUEST = 1; // The request code
  12.     private TextView rra_textview = null;
  13.     
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_return_result);
  18.         rra_textview = (TextView)findViewById(R.id.rra_textview1);
  19.     }

  20.     public void pickContact( View view ) {
  21.      Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
  22.      pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
  23.      startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
  24.     }
  25.     
  26.     @Override
  27.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  28.      // Check which request it is that we're responding to
  29.      if (requestCode == PICK_CONTACT_REQUEST) {
  30.      // Make sure the request was successful
  31.      if (resultCode == RESULT_OK) {
  32.      // Get the URI that points to the selected contact
  33.      Uri contactUri = data.getData();
  34.      // We only need the NUMBER column, because there will be only one row in the result
  35.      String[] projection = {Phone.NUMBER};

  36.      // Perform the query on the contact to get the NUMBER column
  37.      // We don't need a selection or sort order (there's only one result for the given URI)
  38.      // CAUTION: The query() method should be called from a separate thread to avoid blocking
  39.      // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
  40.      // Consider using CursorLoader to perform the query.
  41. //这个查询动作应该放到另外一个线程中,以防止阻塞UI线程,另外可以考虑使用CursorLoader来执行查询
  42.      Cursor cursor = getContentResolver()
  43.      .query(contactUri, projection, null, null, null);
  44.      cursor.moveToFirst();

  45.      // Retrieve the phone number from the NUMBER column
  46.      int column = cursor.getColumnIndex(Phone.NUMBER);
  47.      String number = cursor.getString(column);
  48.      rra_textview.setText("You choose " + number);
  49.      // Do something with the phone number...
  50.      }
  51.      }
  52.     }
  53. }
布局文件为:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:id="@+id/container"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     tools:context="com.example.androidgov.ReturnResultActivity" >

  8.     <Button
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:onClick="pickContact"
  12.         android:text="GetContact" /><!-- 最好放到strings.xml文件中定义 -->

  13.     <TextView
  14.         android:id="@+id/rra_textview1"
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content" />

  17. </LinearLayout>
运行结果截图:

选择联系人后:




阅读(541) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~