@@ -20,23 +20,54 @@ import android.content.BroadcastReceiver
2020import android.content.ComponentName
2121import android.content.Context
2222import android.content.Intent
23+ import android.os.Build
24+ import android.service.chooser.ChooserResult
25+ import android.service.chooser.ChooserResult.CHOOSER_RESULT_COPY
26+ import android.service.chooser.ChooserResult.CHOOSER_RESULT_EDIT
27+ import android.service.chooser.ChooserResult.CHOOSER_RESULT_SELECTED_COMPONENT
28+ import android.service.chooser.ChooserResult.CHOOSER_RESULT_UNKNOWN
2329import android.util.Log
2430import androidx.annotation.RequiresApi
2531import androidx.core.content.IntentCompat
2632
33+
2734private const val TAG = " ShareResultReceiver"
2835
2936@RequiresApi(22 )
3037class ShareResultReceiver : BroadcastReceiver () {
3138
3239 override fun onReceive (context : Context , intent : Intent ) {
33- // This ComponentName represents the Activity that has received the data we shared.
34- val componentName: ComponentName ? = IntentCompat .getParcelableExtra(
35- intent,
36- Intent .EXTRA_CHOSEN_COMPONENT ,
37- ComponentName ::class .java,
38- )
39- // ...
40- Log .d(TAG , " componentName: $componentName " )
40+ if (Build .VERSION .SDK_INT >= 35 ) {
41+ val chooserResult: ChooserResult ? = IntentCompat .getParcelableExtra(
42+ intent,
43+ Intent .EXTRA_CHOOSER_RESULT ,
44+ ChooserResult ::class .java,
45+ )
46+ if (chooserResult != null ) {
47+ Log .i(TAG , " isShortcut: ${chooserResult.isShortcut} " )
48+ Log .i(TAG , " type: ${typeToString(chooserResult.type)} " )
49+ Log .i(TAG , " componentName: ${chooserResult.selectedComponent} " )
50+ } else {
51+ Log .i(TAG , " chooserResult is null" )
52+ }
53+ } else {
54+ // This ComponentName represents the Activity that has received the data we shared.
55+ val componentName: ComponentName ? = IntentCompat .getParcelableExtra(
56+ intent,
57+ Intent .EXTRA_CHOSEN_COMPONENT ,
58+ ComponentName ::class .java,
59+ )
60+ Log .d(TAG , " componentName: $componentName " )
61+ }
62+ }
63+
64+ private fun typeToString (type : Int ): String {
65+ return when (type) {
66+ CHOOSER_RESULT_SELECTED_COMPONENT -> " SELECTED_COMPONENT"
67+ CHOOSER_RESULT_COPY -> " COPY"
68+ CHOOSER_RESULT_EDIT -> " EDIT"
69+ CHOOSER_RESULT_UNKNOWN -> " UNKNOWN"
70+ else -> " UNKNOWN"
71+ }
4172 }
4273}
0 commit comments