-
Notifications
You must be signed in to change notification settings - Fork 469
Add share callback example #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,23 +20,54 @@ import android.content.BroadcastReceiver | |
| import android.content.ComponentName | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.os.Build | ||
| import android.service.chooser.ChooserResult | ||
| import android.service.chooser.ChooserResult.CHOOSER_RESULT_COPY | ||
| import android.service.chooser.ChooserResult.CHOOSER_RESULT_EDIT | ||
| import android.service.chooser.ChooserResult.CHOOSER_RESULT_SELECTED_COMPONENT | ||
| import android.service.chooser.ChooserResult.CHOOSER_RESULT_UNKNOWN | ||
| import android.util.Log | ||
| import androidx.annotation.RequiresApi | ||
| import androidx.core.content.IntentCompat | ||
|
|
||
|
|
||
| private const val TAG = "ShareResultReceiver" | ||
|
|
||
| @RequiresApi(22) | ||
| class ShareResultReceiver : BroadcastReceiver() { | ||
|
|
||
| override fun onReceive(context: Context, intent: Intent) { | ||
| // This ComponentName represents the Activity that has received the data we shared. | ||
| val componentName: ComponentName? = IntentCompat.getParcelableExtra( | ||
| intent, | ||
| Intent.EXTRA_CHOSEN_COMPONENT, | ||
| ComponentName::class.java, | ||
| ) | ||
| // ... | ||
| Log.d(TAG, "componentName: $componentName") | ||
| if (Build.VERSION.SDK_INT >= 35) { | ||
| val chooserResult: ChooserResult? = IntentCompat.getParcelableExtra( | ||
| intent, | ||
| Intent.EXTRA_CHOOSER_RESULT, | ||
| ChooserResult::class.java, | ||
| ) | ||
| if (chooserResult != null) { | ||
| Log.i(TAG, "isShortcut: ${chooserResult.isShortcut}") | ||
| Log.i(TAG, "type: ${typeToString(chooserResult.type)}") | ||
| Log.i(TAG, "componentName: ${chooserResult.selectedComponent}") | ||
| } else { | ||
| Log.i(TAG, "chooserResult is null") | ||
| } | ||
| } else { | ||
| // This ComponentName represents the Activity that has received the data we shared. | ||
| val componentName: ComponentName? = IntentCompat.getParcelableExtra( | ||
| intent, | ||
| Intent.EXTRA_CHOSEN_COMPONENT, | ||
| ComponentName::class.java, | ||
| ) | ||
| Log.d(TAG, "componentName: $componentName") | ||
| } | ||
| } | ||
|
|
||
| private fun typeToString(type: Int): String { | ||
| return when (type) { | ||
| CHOOSER_RESULT_SELECTED_COMPONENT -> "SELECTED_COMPONENT" | ||
| CHOOSER_RESULT_COPY -> "COPY" | ||
| CHOOSER_RESULT_EDIT -> "EDIT" | ||
| CHOOSER_RESULT_UNKNOWN -> "UNKNOWN" | ||
| else -> "UNKNOWN" | ||
| } | ||
|
Comment on lines
+65
to
+71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return when (type) {
CHOOSER_RESULT_SELECTED_COMPONENT -> "SELECTED_COMPONENT"
CHOOSER_RESULT_COPY -> "COPY"
CHOOSER_RESULT_EDIT -> "EDIT"
else -> "UNKNOWN"
} |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -52,7 +52,7 @@ fun ShareSender() { | |||||
| ) { | ||||||
| val context = LocalContext.current | ||||||
| Title(text = "Send various data") | ||||||
| ShareButton(text = "Share plain text") { sharePlainText(context) } | ||||||
| ShareButton(text = "Share plain text (Demonstrates share callback)") { sharePlainText(context) } | ||||||
| ShareButton(text = "Share rich text") { shareRichText(context) } | ||||||
| ShareButton(text = "Share image") { shareImage(context) } | ||||||
| ShareButton(text = "Share multiple images") { shareMultipleImages(context) } | ||||||
|
|
@@ -99,13 +99,30 @@ private val filenames = listOf( | |||||
| "night_highrise.jpg", | ||||||
| ) | ||||||
|
|
||||||
| /** | ||||||
| * Shares a text message and demonstrates how to set up a BroadcastReceiver to be | ||||||
| * notified who has received the data we shared. See [ShareResultReceiver]. | ||||||
| */ | ||||||
| private fun sharePlainText(context: Context) { | ||||||
| context.startActivity( | ||||||
| ShareCompat.IntentBuilder(context) | ||||||
| if (Build.VERSION.SDK_INT >= 22) { | ||||||
| val share = Intent(Intent.ACTION_SEND) | ||||||
| share.setType("text/plain") | ||||||
| share.putExtra(Intent.EXTRA_TEXT,"Hello, world!") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The string
Suggested change
|
||||||
| val pendingIntent = PendingIntent.getBroadcast( | ||||||
| context, 1234, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| Intent(context, ShareResultReceiver::class.java), | ||||||
| PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT | ||||||
| ) | ||||||
| context.startActivity( | ||||||
| Intent.createChooser(share, null, pendingIntent.intentSender) | ||||||
| ) | ||||||
| } else { | ||||||
| context.startActivity(ShareCompat.IntentBuilder(context) | ||||||
| .setType("text/plain") | ||||||
| .setText("Hello, world!") | ||||||
| .createChooserIntent(), | ||||||
| ) | ||||||
| .createChooserIntent() | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private fun shareRichText(context: Context) { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the code more concise and idiomatic, you can use the
letscope function with an elvis operator to handle the nullablechooserResult.