Android Tutorial Your own content - chooser

Hi!

some days ago i shared my ahaShareLibrary and if you like to do your own library with your own code, this small tutorial may help you..

The project need following:
o) Reflection
o) Phone (v 2.10)
o) Main Activity with a placed ListView (ListView1)

You find the example sourcecode attached :)

In Process_Globals put following code:
B4X:
Sub Process_Globals
    Type IntentData (ShareIntent As Object, DetailsIntent As Object)
    Type FileData(Dir As String, FileName As String)
End Sub
FileData is used to, well, store some data about the files you want to add to the intent.
IntentData is used to store 2 Intents in one DataObject. You will see later, why :)

Next go to the Global Decleration
B4X:
Sub Globals
    Dim ListView1 As ListView
    Dim lstFiles, lstFilter As List 
End Sub
ListView1: Our Listview that shows us the Intents
lstFiles: A list that holdes entries of the Type FileData
lstFilter: A list with the Entries (packagenames) which should not be displayed.

Quite easy until now, and it will be easy throug all the turorial :)
Next is Activity_Create:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    lstFiles.Initialize
    lstFilter.Initialize
Both lists have to be initialized...
Lets add an intent-filter, that surpressed all Intents from Synology-Apps:
You can filter out any packagenames, or parts of packagenames.
B4X:
    lstFilter.Add("synology")
Next, lets add a JPG file to the intent. The jpg is stored in File.DirAssets, which is bad, since only our app has access to this directory, sharing it will fail... Lets copy the file from the Assets-Directory to File.DirRootExternal. Don't forget to delete this file when the app closes. (or your sharing is complete).
B4X:
    File.Copy(File.DirAssets,"post2.jpg",File.DirRootExternal, "tmpSharePic.jpg")
Once the file is copied, we create the FileData (the Type declared in Globals) and Add it to lstFiles:
B4X:
    Dim fd As FileData
    fd.Initialize
    fd.Dir = File.DirRootExternal
    fd.Filename = "tmpSharePic.jpg"

    lstFiles.Add(fd)
We start the search for the Intents:
B4X:
    ListIntents("Hy there", "Your intentlist","text/*",lstFilter, lstFiles)
If you don't want to add files or filters, just use NULL instead of the Lists:
B4X:
ListIntents("Hy there", "Your intentlist","text/*",Null,Null)
Now comes the main part, the sub that handles the intentsearch...
B4X:
Sub ListIntents(Subject As String, Text As String, IntentType As String, FilterList As List, FileList As List)
    'Declaration
    Dim pm As  PackageManager
    Dim ShareIntent As Intent
    Dim Action As String

    'Initialize the Intent needed to query for the apps that support the Intent
    
    'If there is more than one file added to the Intent, whe have to change the Intent-Action
    Action = "android.intent.action.SEND"
    If FileList.IsInitialized Then
        If FileList.Size > 1 Then Action = "android.intent.action.SEND_MULTIPLE"
    End If

    ShareIntent.Initialize(Action, "")
    ShareIntent.SetType(IntentType)

    'Start the search for the apps....
    For Each cn As String In pm.QueryIntentActivities(ShareIntent)
        'Try    
        Log(cn)
            Dim SkipPackage As Boolean = False
            Dim i As Int
            
            If FilterList.IsInitialized Then
                For i = 0 To FilterList.Size -1 'Iterate trough the Filter-List
                Dim tmpPackagename As String = FilterList.Get(i)
                    If cn.ToLowerCase.Contains(tmpPackagename.ToLowerCase)  Then SkipPackage = True  'We found a Package to Skip
                Next
            
                If SkipPackage = True Then Continue 'We found a Package to Skip , continue with the next for each (=skip the code below)
            End If
            If cn.ToLowerCase.Contains("facebook") AND cn.ToLowerCase.Contains("orca") Then Continue 'Thats the facebook messenger. We can not cast to it, so its useless..
            
            'Create a temporary Intent
            Dim tmpIntent As Intent 
            tmpIntent.Initialize(Action, "")
            tmpIntent.SetType(IntentType)
            tmpIntent.SetComponent(cn)
            
            'Create a temporary Intent to show App's details
            Dim tmpPackageIntent As Intent
            tmpPackageIntent.Initialize("android.settings.APPLICATION_DETAILS_SETTINGS",CreateUri("package:" & cn.SubString2(0, cn.IndexOf("/"))))

            'Set the Text and Subject values
            If Subject.Length > 0 Then  tmpIntent.PutExtra("android.intent.extra.SUBJECT", Subject)
            If Text.Length > 0 Then  tmpIntent.PutExtra("android.intent.extra.TEXT", Text)

           
            If FileList.IsInitialized Then
                If FileList.Size = 1 Then  'One File is added
                    Dim fd As FileData
                    fd = FileList.Get(0)
                    tmpIntent.PutExtra("android.intent.extra.STREAM", CreateUri("file://" & File.Combine(fd.Dir,fd.FileName)))
                Else If FileList.Size > 1 Then ' More than one file is added
                    Dim lstUri As List
                    lstUri.Initialize 
                    Dim i As Int
                    Dim fd As FileData
                    For i = 0 To FileList.Size -1
                        fd = FileList.Get(i)
                        lstUri.Add(CreateUri("file://" & File.Combine(fd.Dir,fd.FileName)))
                    Next
                    tmpIntent.PutExtra("android.intent.extra.STREAM", lstUri)
                End If
            End If

            'Get the PackageName without the ActivityName
            Dim package As String = cn
            package = package.SubString2(0, package.IndexOf("/"))

            'Get the Icon of the App
            Dim AppIcon As BitmapDrawable  = pm.GetApplicationIcon(package)
            'Get the Name of the app
            Dim AppName As String = pm.GetApplicationLabel(package)
            
            'Creaty myIntentData
            Dim myIntentData As IntentData
            myIntentData.Initialize
            myIntentData.DetailsIntent = tmpPackageIntent
            myIntentData.ShareIntent = tmpIntent
            
                       If cn.ToLowerCase.Contains("facebook") AND cn.ToLowerCase.Contains("katana") Then
                'You can do your FB - Code here...
            End If
 ListView1.AddTwoLinesAndBitmap2(AppName,package,AppIcon.Bitmap,myIntentData)
        Catch
        End Try
    Next

End Sub
You will need a small helper-sub, which is this one:
B4X:
Private Sub CreateUri(uri As String) As Object
    Try
        Dim r As Reflector
        Return r.RunStaticMethod("android.net.Uri", "parse", Array As Object(uri), Array As String("java.lang.String"))
    Catch
        Return ""
    End Try
End Sub
It creates - with the help of reflection - a valid URI for your Files.

Still there? - there is one more last thing...
When the user clicks on the Listentry, we want to start the Intent.
Still remember from the first lines, that we have a Type IntentData and no explanation why we need it? Here is the reason...
ListView1_ItemClick (starting the intent):
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Try
        Dim ShareIntent As Intent
        Dim tmpIntentData As IntentData 
        
        tmpIntentData.Initialize
        tmpIntentData = Value
        ShareIntent = tmpIntentData.ShareIntent   
        StartActivity(ShareIntent)
    Catch
        Msgbox(LastException,"Error")
    End Try
End Sub
ListView1_ItemLongClick (starting App-Details Page)
B4X:
Sub ListView1_ItemLongClick (Position As Int, Value As Object)
    Try
        Dim ShareIntent As Intent
        Dim tmpIntentData As IntentData 
        tmpIntentData.Initialize
        tmpIntentData = Value
        ShareIntent = tmpIntentData.DetailsIntent  'pnl.Tag
        StartActivity(ShareIntent)    
    Catch
        Msgbox(LastException,"Error")
    End Try    
End Sub
I hope you like the Small tutorial :)
 

Attachments

  • shareExample.zip
    44.8 KB · Views: 466

RiverRaid

Active Member
Licensed User
Longtime User
FaceBook-Addition

As the general part is over, i explain how I did the Facebook-Part...

Remember the line:
B4X:
 If cn.ToLowerCase.Contains("facebook") AND cn.ToLowerCase.Contains("katana") Then
    You can do your FB - Code here...
 End If
I did a very, very simple thing...
B4X:
If cn.ToLowerCase.Contains("facebook") AND cn.ToLowerCase.Contains("katana") Then
    myIntentData.ShareIntent = Null
End If
And then, in ListView1_ItemClick:
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Try
        Dim ShareIntent As Intent
        Dim tmpIntentData As IntentData 
        
        tmpIntentData.Initialize
        tmpIntentData = Value
        
        If tmpIntentData.ShareIntent = Null Then
            Msgbox ("Facebook","...") 'Your FaceBookCode Here
        Else
            ShareIntent = tmpIntentData.ShareIntent  
            StartActivity(ShareIntent)
        End If
    Catch
        Msgbox(LastException,"Error")
    End Try
End Sub
You can now easely replace the Msgbox("Facebook","...") - Part with your Facebook - code (or by code from the excellent FB-Tutorial from NJDude)
 
Last edited:
Top