@@ -20,23 +20,54 @@ import android.content.BroadcastReceiver
20
20
import android.content.ComponentName
21
21
import android.content.Context
22
22
import 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
23
29
import android.util.Log
24
30
import androidx.annotation.RequiresApi
25
31
import androidx.core.content.IntentCompat
26
32
33
+
27
34
private const val TAG = " ShareResultReceiver"
28
35
29
36
@RequiresApi(22 )
30
37
class ShareResultReceiver : BroadcastReceiver () {
31
38
32
39
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
+ }
41
72
}
42
73
}
0 commit comments