From dd31c628dd73e5f24ffe38fa61217a1fd5db8b74 Mon Sep 17 00:00:00 2001 From: Clara Bayarri Date: Fri, 15 Apr 2016 11:48:32 +0100 Subject: [PATCH] Expand existing v4 Fragment receive result sample to IntentSender Now that we support startIntentSenderForResult, this change expands the existing startActivityForResult sample to support both. Bug: 27700608 Change-Id: Ib8e88b144a34b63a947257547e7453d5f0018835 --- .../res/layout/receive_result.xml | 23 ++++++++++---- samples/Support4Demos/res/values/strings.xml | 5 ++-- .../app/FragmentReceiveResultSupport.java | 30 +++++++++++++++++-- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/samples/Support4Demos/res/layout/receive_result.xml b/samples/Support4Demos/res/layout/receive_result.xml index 5deb2ac4a..dbaa29bd3 100644 --- a/samples/Support4Demos/res/layout/receive_result.xml +++ b/samples/Support4Demos/res/layout/receive_result.xml @@ -21,17 +21,20 @@ + android:layout_width="match_parent" + android:layout_height="match_parent"> + + diff --git a/samples/Support4Demos/res/values/strings.xml b/samples/Support4Demos/res/values/strings.xml index 2405ba598..bd6b31bb8 100644 --- a/samples/Support4Demos/res/values/strings.xml +++ b/samples/Support4Demos/res/values/strings.xml @@ -30,8 +30,9 @@ Pick a result to send, or BACK to cancel. Corky Violet - Press the button to get an activity result, which will be displayed here: - Get Result + Press the buttons below to get an activity or intent sender result, which will be displayed here: + Get Activity Result + Get IntentSender Result diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentReceiveResultSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentReceiveResultSupport.java index f63826cb4..9e2effe49 100644 --- a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentReceiveResultSupport.java +++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentReceiveResultSupport.java @@ -19,6 +19,8 @@ package com.example.android.supportv4.app; import com.example.android.supportv4.app.SendResult; import com.example.android.supportv4.R; +import android.app.PendingIntent; +import android.content.IntentSender; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; @@ -55,8 +57,9 @@ public class FragmentReceiveResultSupport extends FragmentActivity { } public static class ReceiveResultFragment extends Fragment { - // Definition of the one requestCode we use for receiving resuls. + // Definition of the one requestCode we use for receiving results. static final private int GET_CODE = 0; + static final private int GET_INTENT_SENDER_CODE = 1; private TextView mResults; @@ -69,6 +72,25 @@ public class FragmentReceiveResultSupport extends FragmentActivity { } }; + private OnClickListener mIntentSenderListener = new OnClickListener() { + public void onClick(View v) { + // Start the intent sender whose result we want to retrieve. The + // result will come back with request code GET_INTENT_SENDER_CODE. + Intent intent = new Intent(getActivity(), SendResult.class); + PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), + GET_INTENT_SENDER_CODE, intent, 0); + try { + startIntentSenderForResult(pendingIntent.getIntentSender(), + GET_INTENT_SENDER_CODE, null, 0, 0, 0, null); + } catch (IntentSender.SendIntentException e) { + // We will be adding to our text. + Editable text = (Editable)mResults.getText(); + text.append(e.getMessage()); + text.append("\n"); + } + } + }; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -93,6 +115,8 @@ public class FragmentReceiveResultSupport extends FragmentActivity { // Watch for button clicks. Button getButton = (Button)v.findViewById(R.id.get); getButton.setOnClickListener(mGetListener); + Button intentSenderButton = (Button) v.findViewById(R.id.get_intentsender); + intentSenderButton.setOnClickListener(mIntentSenderListener); return v; } @@ -106,11 +130,13 @@ public class FragmentReceiveResultSupport extends FragmentActivity { // You can use the requestCode to select between multiple child // activities you may have started. Here there is only one thing // we launch. - if (requestCode == GET_CODE) { + if (requestCode == GET_CODE || requestCode == GET_INTENT_SENDER_CODE) { // We will be adding to our text. Editable text = (Editable)mResults.getText(); + text.append((requestCode == GET_CODE) ? "Activity " : "IntentSender "); + // This is a standard resultCode that is sent back if the // activity doesn't supply an explicit result. It will also // be returned if the activity failed to launch.