Google Android開発者ドキュメントシリーズ-他のアプリケーションと対話してActivityを取得した結果


Getting a Result from an Activity(活動中に戻った結果を取得)
このシリーズの文章は私がGoogleの開発者の文書を学ぶ時Googleの翻訳と自身の理解を結び付けて編纂したので、Androidの開発を学ぶ友達に対していくらかの便利さを持ってくることを望んで、個人の翻訳のレベルが有限なため、内容は原文と翻訳を含んで、閲覧者が理解を結び付けてほしいです。ここに感謝しますhttp://android.xsoftlab.net/提供する鏡像は、転載者に出典を明記してもらいたいです。http://blog.csdn.net/u014031072/article/details/51538673最新のブログを見るには便利です。
Starting another activity doesn’t have to be one-way.You can also start another activity and receive a recelt back.To receive a receult,call startActivityForResot(instead of startActivity)は、いつも一方的な活動を開始しません。他のアクティビティを起動して、結果を受信することもできます。結果を受信するには、startActivity ForResoult()を呼び出します。
For example,your app can start a camera ap and receive the capture photo as a reult.Or,You might start the People ap in order for the user to select a and you'll receive the contant deils.orts.orts.orts.ortsとしてアプリケーションを開始します。あるいは、連絡先アプリケーションを起動して、連絡先をユーザに選択させ、結果として受信した連絡先を返すことができます。
Of course,the activity that reponds must be designed to return a return.When it does,it sends the relt as Intent oject.Your activity receives it in the ActivityResoult(calback)はもちろん、イベントの結果を返します。設定が完了すると、設定した結果を別のintentオブジェクトとして送ります。あなたのアクティブなインターフェースはそのonActivityResoult()コールバック方法でリターンの結果を受信します。
Note:You can use explicit or implicit intensts when you call startActivityForResoult().When starting one of your own activities to receive a recelt,you Shuld use an explicit intent to ensure thatを使用することができます。あなたが自分の活動インターフェースを起動してリターン結果を受信するときは、期待されるリターン値を受信することを確実にするために、明示的な形式を使用するべきです。
Start the Activity(起動活動)
The’s nothing special about the Intent object you use when starting an activity for a result,but you do need to pass and additititional integer argment to the startActivityForResoult()method.あなたが起動して1つの返送結果を受け取った時には何も伝えられませんでした。しかし、あなたはstartActivityForResoultに追加の整数パラメータを渡さなければなりません。
The integer argment is a「request code」that dentifries your request.When You receive the receive the recelt Intent,the calback provides the same request code sour ap can properly dentifty.このパラメータのために使用してください。戻るintent結果を受信すると、コールバックは同じ要求コードを提供し、このリターン結果を正しく認識し、どのように操作するかを決定することができます。
For example、here’s how to start an activity that allows the user to pick a contact:例えば、次はどのように活動を開始し、ユーザーが連絡先を選択することができますか?
static final int PICK_CONTACT_REQUEST = 1;  // The request code    
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers           (      )
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
Receive the Result(戻りの結果を取得)
When the user is done with the subsequent activity and returns,the system cars your activity's onActivityResoult()method.This method includes thargments:ユーザーがこの一連の活動を完了すると、イベントの設定方法が返ってきます。この方法は三つのパラメータを含む。
1.The request code you passed to startActivityForResoult().1.startActivityForResoult()に送る方法の要求コード。
2.A result code specified by the second activity.This is ether RESULT_OK if the operation was success ful or RESULT_CANCELLED if the user backed out or the operation failed for some reason.2.一つは第二のアクティブインターフェース(起動されるアクティブインターフェース)で定義される結果コードです。操作が成功した時の値はRESULT_です。OKです。ユーザーが退出または操作に失敗した場合、その値はRESULT_です。CANCELLED
3.An Intent that carries the result data.3.戻り結果を含むintent
For example,here’s how you can handlee the result for the「pick a contact」intent:例えば、連絡先intentの返却結果を選択する操作の例:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    //         
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        //      
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            //          
            // The Intent's data Uri identifies which contact was selected.
            //intent Uri              
            // Do something with the contact here (bigger example below)
            //          (       )
        }
    }
}
In this example、the result Intent returned by Android’s Contact or People app provides a content Uri that dentifies the contact the user selected.
この例では、Androidの連絡先または連絡先アプリケーションによって返された結果、intentは、ユーザによって選択された連絡先を識別するためのUriコンテンツを提供する。
Inordeder to succcccccccccefully hadle the reult,you must undedestand what the formas of the rerererererererererererererererererererererererererereremas.aps includ wiwith Andrdratdrattdrafffttttthethethererererererererererererererererererererererererererererererereaaaaattttttttttttttaaaaaaaaaaattttthethethethethethethethethethethethethethethethethethethethethethethethep(Contacs ap on some older versions)always returns a returns with the content URI that ideantifies the selected contat,and the Camera ap returns a a Bitmap in the「data」extra成功した操作のために戻った結果は、返ってきた結果intentのデータフォーマットを理解しなければなりません。戻り結果を設定するアクティブインターフェースは、あなた自身が定義したものです。戻り結果を設定するアクティブインターフェースはAndroidプラットフォーム自身のAPIが提供した場合、特定のフォーマットの結果データが得られます。例えば、連絡先アプリケーションです。(いくつかの古いバージョンの連絡先アプリケーション)は、選択された連絡先を識別するURI結果を返します。カメラアプリケーションはintentの追加データ「data」の中からBitmapデータを返します。
Bonus:Read the contact data(追加:連絡先データの読み込み)
The code abobobove showhohow to get a rererelt from the People ap doesn't go into details abouthohow to to actually read the datafrom the rerereult,because it requires advancnced discscsion autconteteteststinindedededevidededededededededededededededededededededededededededededededededededererererererererererererererererererererererererererererererererererererererererererererererererererererererererefrom the selected cワンポイント:上のコードは連絡先アプリケーションからどのように戻り結果を取得するかを示していますが、戻り結果のデータをどのように正しく読み込むかは詳細に示されていません。これはコンテンツプロバイダに関するさらなる議論が必要です。しかし、もし気になることがあれば、以下にいくつかの追加コードが表示されます。返信結果から選択された連絡先の携帯番号を調べられます。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to
    //               
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        //        
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            //           URI
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            //     NUMBER    ,        
            String[] projection = {Phone.NUMBER};
            // Perform the query on the contact to get the NUMBER column
            //       NUMBER    
            // We don't need a selection or sort order (there's only one result for the given URI)
            //           (   URI       )
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
              //  :query()             ,            UI  (          ,         。)
            // Consider using CursorLoader to perform the query.
            //    CursorLoader    。
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();
            // Retrieve the phone number from the NUMBER column
            // NUMBER        
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);
            // Do something with the phone number...
            //        
        }
    }
}
Note:Before Android 2.3(API level 9)、performing a query on the Contacs Provider(like the one shown above)requires that your ap declare the READuCONTACT S permission(security and Permissions)However,begining with Android 2.3,the Contacs/People ap grantsyourap app a tempororary permission to read from the Contitrereturns.The temporary permisisisisisisisisisisipapapapapapanese thethererererererererererererereconconconconconconconconconconconconconconconconconconconconconconconconcondededes s s s s PrPrinininininininininininininininininininininininininininininininininininininininininininconconconconconconconconconconconconconconless you do declare the READUCONTACS permission.注:Android 2.3の(API 9級)前に、連絡先提供者の中でクエリー操作(上に示すように)を実行して、あなたのアプリケーションにREADUCONTACS権限を要求します(安全と権限を参照してください)しかし、Android 2.3から連絡先提供者のデータを読み取り、結果を返すと連絡先アプリケーションがあなたのアプリケーションに一時的な許可を与えます。一時的な許可は特定の連絡先要求にのみ適用されますので、intentを確認することはできません。のURIが指定した連絡先以外の連絡先は、READUCONTACSの権限を宣言していない限り。