I want to know the package name of app clicked from android sharsheet. Developer.android.com says to use a pending with Intent.createchooser. And we can get package name in onReceive method of broadcast receiver that we use in pending Intent. My Main Activity only has a button that starts a sharing intent and opens android sharesheet.
package com.upstox.android.shubhampoc
import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.upstox.android.shubhampoc.databinding.ActivityMainBinding
import com.upstox.android.shubhampoc.share.SharingNotificationReceiver
class MainActivity : AppCompatActivity() {
private var _binding: ActivityMainBinding? = null
private val binding get() = checkNotNull(_binding)
private val sharingNotificationReceiver = SharingNotificationReceiver()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setClickListeners()
}
override fun onStart() {
super.onStart()
registerReceiver(sharingNotificationReceiver, IntentFilter())
}
override fun onStop() {
super.onStop()
unregisterReceiver(sharingNotificationReceiver)
}
private fun setClickListeners() {
binding.btnShare.setOnClickListener {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "Sharing from a POC App")
type = "text/plain"
}
val receiveIntent = Intent(applicationContext, SharingNotificationReceiver::class.java)
receiveIntent.putExtra("check", "value received")
val pendingIntent = PendingIntent.getBroadcast(
applicationContext,
25,
receiveIntent,
PendingIntent.FLAG_IMMUTABLE
)
val shareIntent =
Intent.createChooser(sendIntent, "Share Text", pendingIntent.intentSender)
startActivity(shareIntent)
}
}
}
Here is my Broadcast receiver.
package com.upstox.android.shubhampoc.share
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.Intent.EXTRA_CHOSEN_COMPONENT
class SharingNotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val clickedComponent : ComponentName? = intent?.getParcelableExtra(EXTRA_CHOSEN_COMPONENT);
println("shubhamnoti intent : $clickedComponent")
println("shubhamnoti intent : ${intent?.getStringExtra("check")}")
}
}
And my android.manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.upstox.android.shubhampoc">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ShubhamPOC"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.ShubhamPOC.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".share.SharingNotificationReceiver"
android:exported="false">
</receiver>
</application>
</manifest>
The problem I am facing is I am getting Intent.extras as null in broadcast receiver. Is there a way to get the clicked app component name from android sharesheet.
I am expecting the component name of app clicked from android sharesheet. But Intent.extras is null in broadcast receiver and if i pass a custom extra just for checking then intent.extras has only that custom extra in it.
To be precise i am expecting override fun onReceive(context: Context, intent: Intent) { ... val clickedComponent : ComponentName = intent.getParcelableExtra(EXTRA_CHOSEN_COMPONENT); }
as mentioned in android documentation.