Android Question [SOLVED] How to create shortcut to URL from B4A?

Juanet

Member
Licensed User
Longtime User
Hello all!

I read this thread: https://www.b4x.com/android/forum/t...-directly-from-within-your-application.17559/

But this is to create a shortcut to some app (if im understand well).

I have a website that is prepared so that a shortcut can be created from the same site (and it works), my idea is to be able to create it with the click of a button from an application (B4A) that I am developing and also verify that it has only been created once, I imagine that it is possible, although since I have no idea, that's why I ask.

Thank you all for your help!
 

Juanet

Member
Licensed User
Longtime User
Im see one response in stackoverflow that maybe is what would be what I would be wanting to do, since I can not find anything similar in the forum that helps me, since all responses here are for create shortcuts for native apps, not URL's , someone could tell me how to move this to B4A, or how can create shortcut URL's?

Thanks in advance!


C-like:
private void setShortcut(String url) {
        String query = Uri.encode(url, "UTF-8");
        browserIntent = new Intent(CATEGORY_BROWSABLE, Uri.parse(Uri.decode(query)));
        browserIntent.setAction(ACTION_VIEW);

        if (ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext())) {
            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(getApplicationContext(), "1")
                    .setIntent(browserIntent) // !!! intent's action must be set on oreo
                    .setShortLabel("Test")
                    .setIcon(IconCompat.createWithResource(getApplicationContext(), R.drawable.bg))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcutInfo, null);
        } else {
            Toast.makeText(getApplicationContext(),"launcher does not support short cut icon",Toast.LENGTH_LONG).show();
        }
    }
 
Upvote 0

Juanet

Member
Licensed User
Longtime User
Thanks Erel,

Im trying with a modified version of the code, but i have an error, next are the code:

B4X:
Sub RequestPinShortcut (Update As Boolean)

    Dim ctxt As JavaObject
    ctxt.InitializeContext

    Dim ShortcutManagerCompat As JavaObject
    ShortcutManagerCompat.InitializeStatic("androidx.core.content.pm.ShortcutManagerCompat")

    Dim supported As Boolean = ShortcutManagerCompat.RunMethod("isRequestPinShortcutSupported", Array(ctxt))
    If supported Then

        Dim builder As JavaObject
        builder.InitializeNewInstance("androidx.core.content.pm.ShortcutInfoCompat.Builder", Array(ctxt, "example.com"))
        builder.RunMethod("setShortLabel", Array("Example WebSite"))
        builder.RunMethod("setIcon", Array(LoadBitmap(File.DirAssets, "android-icon-72x72.png")))

        Dim in As Intent
        in.Initialize(in.ACTION_VIEW, ParseUri("https://example.com/"))
        in.AddCategory("android.intent.category.CATEGORY_BROWSABLE")
        'in.SetComponent(Application.PackageName & "/.main") 'lower case

        builder.RunMethod("setIntent", Array(in))

        Dim info As JavaObject = builder.RunMethod("build", Null)
        If Update Then
            Dim infos As List = Array(info)
            Log("Update successfully? " & ShortcutManagerCompat.RunMethod("updateShortcuts", Array(ctxt, infos)))
        Else
            ShortcutManagerCompat.RunMethod("requestPinShortcut", Array(ctxt, info, Null))
        End If
    End If
End Sub

Sub ParseUri(s As String) As Object
    Dim r As Reflector
    Return r.RunStaticMethod("android.net.Uri", "parse", Array As Object(s), Array As String("java.lang.String"))
End Sub

Thats is the error, line 332 correspond to:
ShortcutManagerCompat.InitializeStatic("androidx.core.content.pm.ShortcutManagerCompat")

Diff:
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 332 (B4XMainPage)
java.lang.ClassNotFoundException: androidx.core$content$pm$ShortcutManagerCompat
    at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:289)
    at anywheresoftware.b4j.object.JavaObject.InitializeStatic(JavaObject.java:75)
    at com.laradio.fm.b4xmainpage._requestpinshortcut(b4xmainpage.java:375)
    at com.laradio.fm.b4xmainpage._btn6_click(b4xmainpage.java:345)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7464)
    at android.view.View.performClickInternal(View.java:7441)
    at android.view.View.access$3600(View.java:819)
    at android.view.View$PerformClick.run(View.java:28365)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:7843)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)


And thats is the code in manifest:
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="31"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

The idea behind is not ask to the user, simply add a shorcut to the website, thanks so much!
 
Upvote 0

Juanet

Member
Licensed User
Longtime User
DonManfred, many thanks,

Added your line to project and change thats due to another error:
B4X:
'change this line in Sub RequestPinShortcut:
builder.RunMethod("setIcon", Array(LoadBitmap(File.DirAssets, "android-icon-72x72.png")))

'for thats:
builder.RunMethod("setIcon", Array(CreateIconFromBitmap))

Private Sub CreateIconFromBitmap() As Object
    Dim ic As JavaObject
    Return ic.InitializeStatic("androidx.core.graphics.drawable.IconCompat").RunMethod("createWithBitmap", Array(LoadBitmap(File.DirAssets, "android-icon-72x72.png")))
End Sub

'call from btn like this:

Private Sub btn6_Click
    RequestPinShortcut(False)
End Sub

Now shortcut are created but no loads the website instead try to load the app

Surely im missing something, can you help with thats?

Thanks you in advance!
 
Upvote 0

Juanet

Member
Licensed User
Longtime User
This post is to help others:

The correct way to install and URL shortcut is with that code:

B4X:
#AdditionalJar: androidx.core:core

'call from btn like this:
Private Sub btn6_Click
    RequestPinShortcut
End Sub

Sub RequestPinShortcut ()
    Dim ctxt As JavaObject
    ctxt.InitializeContext

    Dim ShortcutManagerCompat As JavaObject
    ShortcutManagerCompat.InitializeStatic("androidx.core.content.pm.ShortcutManagerCompat")
    Dim supported As Boolean = ShortcutManagerCompat.RunMethod("isRequestPinShortcutSupported", Array(ctxt))
    If supported Then
        
        Dim in As Intent
        in.Initialize2("https://example.com/", 1)

        Dim builder As JavaObject
        builder.InitializeNewInstance("androidx.core.content.pm.ShortcutInfoCompat.Builder", Array(ctxt, "1"))
        builder.RunMethod("setShortLabel", Array("Example Demo"))
        builder.RunMethod("setIcon", Array(CreateIconFromBitmap))
        builder.RunMethod("setIntent", Array(in))

        Dim info As JavaObject = builder.RunMethod("build", Null)
        ShortcutManagerCompat.RunMethod("requestPinShortcut", Array(ctxt, info, Null))
    End If
End Sub

Private Sub CreateIconFromBitmap() As Object
    Dim ic As JavaObject
    Return ic.InitializeStatic("androidx.core.graphics.drawable.IconCompat").RunMethod("createWithBitmap", Array(LoadBitmap(File.DirAssets, "android-icon-72x72.png")))
End Sub

Don't forget to add this line to manifest:

B4X:
<uses-permission android:name="com.android.launcher.permission.CREATE_SHORTCUT"/>
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…