Android Question How to know the app chosen by user in an intent?

Alpandino

Member
Licensed User

fredo

Well-Known Member
Licensed User
Longtime User
I didn't had time to deal with this question

OK, while fiddling around with the informations I figured out a way to make a custom chooser for an ACTION_SEND Intent that excludes the own app.

The Manifest that makes the app availabe as SENDER, so we can test to avoid it in the chooser:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

'
AddApplicationText(
<activity android:name="main" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
            <data android:mimeType="text/*" />
        </intent-filter>
</activity>
)

B4X:
Sub SendChooser(strPath As String, strFile As String)
  
    uriX.Parse("file://" & File.Combine(strPath, strFile))
  
    Dim mapSkipForList As Map
    mapSkipForList.Initialize
    mapSkipForList.Put("com.google.android.apps.maps", True) ' Example

    ' Comment the next line out to proof that your app can be skipped or not
    mapSkipForList.Put(Application.PackageName, True) ' <<--- <<--- <<--- This is your own package name

    '
    Dim strPackageName As String = ""
    Dim pm As PackageManager
    Dim Intent1 As Intent
    Intent1.Initialize(Intent1.ACTION_SEND, "file://")
    Intent1.SetType("image/*")
    '
    Log(" ")
    For Each cn As String In pm.QueryIntentActivities(Intent1)
        strPackageName = cn.SubString2(0, cn.IndexOf("/"))
        If mapSkipForList.ContainsKey(strPackageName) Then
            Log("    >> Skipped strPackageName=" & strPackageName)  
        Else
            Log("Used strPackageName=" & strPackageName)  
            Dim bdw As BitmapDrawable = pm.GetApplicationIcon(strPackageName)
            lv.AddTwoLinesAndBitmap2(pm.GetApplicationLabel(strPackageName), "", bdw.Bitmap, cn )
        End If
    Next
    Log(" ")
  
End Sub


Sub LvChooser_ItemClick (Position As Int, Value As Object)
   Log($"LvChooser_ItemClick, Position=${Position}, Value=${Value}"$)
  
   Dim Intent1 As Intent
   Intent1.Initialize(Intent1.ACTION_SEND , "file://")
   Intent1.SetComponent(Value)
   Intent1.SetType("image/*")
   Intent1.PutExtra("android.intent.extra.STREAM", uriX)  ' <<-- "uriX" contains the path yout setup to your file with uriX.Parse(...)
  StartActivity(Intent1)
 

Attachments

  • ExampleContentChooserSkipOwnPackage.zip
    12 KB · Views: 280
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
OK, while fiddling around
Thank you for this example!
BTW: I got a exception without this line
B4X:
    mapSkipForList.Put("com.android.nfc",True)

The exception was

 
Upvote 0

fredo

Well-Known Member
Licensed User
Longtime User
..The exception was...
java.lang.ClassCastException: com.samsung.android.sdk.spr.drawable.SprDrawable cannot be cast to android.graphics.drawable.BitmapDrawable...

Seems that the Samsung device has an inedible "SpriteDrawable" for NFC.

Since I can't reproduce the error to find a fix I would think that a dirty Try-Catch could help as a temporary workaround until a profound solution is available.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…