Android Question How do you make a system shortcut?

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
'START Shortcut creation
Sub ShortcutActivity'call this to ask for a shortcut
	Dim j As Intent , j2 As Intent 
	Dim ACTION_PICK_ACTIVITY As String = "android.intent.action.PICK_ACTIVITY"
	Dim ACTION_CREATE_SHORTCUT As String = "android.intent.action.CREATE_SHORTCUT"
	Dim EXTRA_INTENT As String = "android.intent.extra.INTENT"
	Dim EXTRA_TITLE As String = "android.intent.extra.TITLE" 
	
	j.Initialize(ACTION_PICK_ACTIVITY, "")
	j2.Initialize(ACTION_CREATE_SHORTCUT,"")
	j.PutExtra(EXTRA_INTENT, j2)
	j.PutExtra(EXTRA_TITLE, "Create a system shortcut")
	
	StartActivityForResult(j)
	
	'Intent j;
	'j = new Intent(Intent.ACTION_PICK_ACTIVITY);
	'j.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
	'j.putExtra(Intent.EXTRA_TITLE, "Create a Shortcut");
	'StartActivityForResult(j, choose_app_for_shortcut);
End Sub

Sub StartActivityForResult(i As Intent)
   Dim jo As JavaObject = GetBA
   SkipOne=True
   ion =  jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
   jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub
Sub GetBA As Object
   Dim jo As JavaObject
   Dim cls As String = Me
   cls = cls.SubString("class ".Length)
   jo.InitializeStatic(cls)
   Return jo.GetField("processBA")
End Sub

Sub ion_Event (MethodName As String, Args() As Object) As Object
   	'Dim I As Intent 
   	If Args(0) = -1 Then 'resultCode = RESULT_OK
   		'Log("MethodName: " & MethodName)'=ResultArrived
		'Arg 1: Intent { act=android.intent.action.CREATE_SHORTCUT cmp=com.google.android.email/com.android.email2.ui.CreateShortcutActivityEmail }
		StartActivity(Args(1))
		
   	End If
   	Return Null
End Sub
'END shortcut creation

AddReceiverText(stimer,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>)

I use the above code and manifest entry, but for most apps won't trigger the com.android.launcher.action.INSTALL_SHORTCUT event.
 

DonManfred

Expert
Licensed User
Longtime User
if i remember correctly starting from android 19 (KitKat) only the defined Launcher-app are allowed to install Shortcuts.
 
Upvote 0
Top