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:
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
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:
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.
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).
Once the file is copied, we create the FileData (the Type declared in Globals) and Add it to lstFiles:
We start the search for the Intents:
If you don't want to add files or filters, just use NULL instead of the Lists:
Now comes the main part, the sub that handles the intentsearch...
You will need a small helper-sub, which is this one:
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):
ListView1_ItemLongClick (starting App-Details Page)
I hope you like the Small tutorial
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
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
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
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")
B4X:
File.Copy(File.DirAssets,"post2.jpg",File.DirRootExternal, "tmpSharePic.jpg")
B4X:
Dim fd As FileData
fd.Initialize
fd.Dir = File.DirRootExternal
fd.Filename = "tmpSharePic.jpg"
lstFiles.Add(fd)
B4X:
ListIntents("Hy there", "Your intentlist","text/*",lstFilter, lstFiles)
B4X:
ListIntents("Hy there", "Your intentlist","text/*",Null,Null)
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
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
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
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