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

Alpandino

Member
Licensed User

DonManfred

Expert
Licensed User
Longtime User
No, he want to know the APP which the user selected to share with....
 
Upvote 0

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: 288
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

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Used strPackageName=com.samsung.android.app.memo
Used strPackageName=com.android.mms
Used strPackageName=com.samsung.android.app.FileShareClient
Used strPackageName=com.android.bluetooth
Used strPackageName=com.google.android.talk
>> Skipped strPackageName=com.google.android.apps.maps
Used strPackageName=com.google.android.apps.photos
Used strPackageName=com.samsung.android.email.provider
Used strPackageName=com.android.nfc
main_sendchooser (java line: 457)
java.lang.ClassCastException: com.samsung.android.sdk.spr.drawable.SprDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
at b4a.example.contchoos.main._sendchooser(main.java:457)
at b4a.example.contchoos.main._activity_resume(main.java:358)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at b4a.example.contchoos.main.afterFirstLayout(main.java:108)
at b4a.example.contchoos.main.access$000(main.java:17)
at b4a.example.contchoos.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 
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.
30-09-_2016_17-43-59.jpg
 
Upvote 0
Top