Android Code Snippet Add pinned shortcut

Discussion in 'Code Snippets' started by Erel, Aug 18, 2019.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    #AdditionalJar: androidx.core:core

    Sub RequestPinShortcut (id As String, icon As Bitmap, ShortLabel As Object)
       
    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, id))
           builder.RunMethod(
    "setShortLabel"Array(ShortLabel))
           builder.RunMethod(
    "setIcon"Array(CreateIconFromBitmap(icon)))
           
    Dim in As Intent
           
    in.Initialize(in.ACTION_MAIN, "")
           
    in.SetComponent(Application.PackageName & "/.main"'lower case
           builder.RunMethod("setIntent"Array(in))
           ShortcutManagerCompat.RunMethod(
    "requestPinShortcut"Array(ctxt, builder.RunMethod("build"Null), Null))
       
    End If
    End Sub

    Private Sub CreateIconFromBitmap(bmp As BitmapAs Object
       
    Dim ic As JavaObject
       
    Return ic.InitializeStatic("androidx.core.graphics.drawable.IconCompat").RunMethod("createWithBitmap"Array(bmp))
    End Sub
    Depends on JavaObject. Request AndroidX SDK.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice