---
title: "从Android Activity开始"
author: "地球小野花"
date: "2014-01-17T15:17:54Z"
tags: []
categories: ["移动开发"]
slug: "android-activity"
draft: false
lastmod: "2014-01-17T15:18:57Z"
id: 2005682
url: "https://android-sz.iteye.com/blog/2005682"
---

从Android Activity开始

（转载注明出处）

 

       至于Activity是什么，不做详细述说可参考 Google Android 开发文档。生命在于运动，Activity是怎么动起来的？它的“能量“又是怎么传递的？看两个东西

 

1.Starting an Activity

2.Starting an activity for a result

 

详细：

 

1 Starting an Activity

startActivity Param: intent

Extra:(参考：Intents and Intent Filters)

Intent 包括 activity action data(small amounts of data)

 

e.g

 

```
//简单开始一个Activity
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
```

 

 

```
/*
 *发送email
 *Intent.Action_SEND <-添加发送事件 启动send Activity
 *Intent.EXTRA_EMAIL <-添加邮件事件 IF case Email
 */
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
startActivity(intent);
```

 

 

 

 

2 Starting an activity for a result

a.A Activity:startActivityForResult  Param:intent requestcode

b.B Activity:setResult Param:resultCode || setResult Param: resultcode data

c.A Activity:onActivityResult Param: requestCode resultCode data 

 

e.g

 

 

```
//请求获取系统联系人
private void pickContact() {
    // Create an intent to "pick" a contact, as defined by the content provider URI
    Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT_REQUEST);
}
```

 

 

 

 

```
//返回获取到的联系人
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // If the request went well (OK) and the request was PICK_CONTACT_REQUEST
    if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {
        // Perform a query to the contact's content provider for the contact's name
        Cursor cursor = getContentResolver().query(data.getData(),
        new String[] {Contacts.DISPLAY_NAME}, null, null, null);
        if (cursor.moveToFirst()) { // True if the cursor is not empty
            int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
            String name = cursor.getString(columnIndex);
            // Do something with the selected contact's name...
        }
    }
}
```

 

 

 

 参考资料:

 Android 开发文档