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
This commit is contained in:
@@ -21,17 +21,20 @@
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
|
||||
android:gravity="center_horizontal"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent" android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:paddingBottom="4dip"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
android:text="@string/receive_result_instructions"/>
|
||||
|
||||
<TextView android:id="@+id/results"
|
||||
android:layout_width="match_parent" android:layout_height="10dip"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="10dip"
|
||||
android:layout_weight="1"
|
||||
android:paddingBottom="4dip"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
@@ -40,11 +43,19 @@
|
||||
</TextView>
|
||||
|
||||
<Button android:id="@+id/get"
|
||||
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:text="@string/receive_result_result">
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:text="@string/receive_result_result">
|
||||
<requestFocus />
|
||||
</Button>
|
||||
|
||||
<Button android:id="@+id/get_intentsender"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:text="@string/receive_result_intentsender">
|
||||
</Button>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -30,8 +30,9 @@
|
||||
<string name="pick_result">Pick a result to send, or BACK to cancel.</string>
|
||||
<string name="corky">Corky</string>
|
||||
<string name="violet">Violet</string>
|
||||
<string name="receive_result_instructions">Press the button to get an activity result, which will be displayed here:</string>
|
||||
<string name="receive_result_result">Get Result</string>
|
||||
<string name="receive_result_instructions">Press the buttons below to get an activity or intent sender result, which will be displayed here:</string>
|
||||
<string name="receive_result_result">Get Activity Result</string>
|
||||
<string name="receive_result_intentsender">Get IntentSender Result</string>
|
||||
|
||||
<!-- Fragment API -->
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user