Italian Icona sul desktop.

luciano deri

Active Member
Licensed User
Longtime User
Salve. Alla primo avvio dell'app voglio creare un l'icona sul desktop principale con la seguente sub (trovata sul forum) chiamata nel create
B4X:
Sub IconaDesktop
    Dim PrefMgr As PreferenceManager
   If PrefMgr.GetBoolean("shortcutinstalled") Then
      Return
   End If
  
   Dim shortcutIntent As Intent
   shortcutIntent.Initialize("", "")
   shortcutIntent.SetComponent("com.multidataitalia.siscoapp/.main") ' Put the app package name here
  
   Dim In As Intent
   In.Initialize("", "")
   In.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
   In.PutExtra("android.intent.extra.shortcut.NAME", "SiscoApp") ' Put you're application name here
   In.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "iconasiscoapp.jpg")) ' If you have the icon file in the assets, you just need any bitmap here. Best is 72x72 pixels because launchers do not scale. You might want to experiment a little bit with size
  
   ' Here starts the real action that all the other posts had missing:
        ' How to get this OUT of your app to the launcher
        ' You need to send a broadcast, that's all :-)
   In.Action = "com.android.launcher.action.INSTALL_SHORTCUT"
  
   Dim p As Phone
   p.SendBroadcastIntent(In)
  
   PrefMgr.SetBoolean("shortcutinstalled", True)
End Sub

Il risultato è pessimo: l'icona (72x72) risuta tagliata e comunque più piccola di quella che sarebbe creata manualmente. C'è il modo per fare qualcosa di meglio?
 

Attachments

  • screen.png
    screen.png
    508.7 KB · Views: 372

LucaMs

Expert
Licensed User
Longtime User
Mi butto, su qualcosa che non conosco affatto, sperando che qualcuno che ci sia già passato ti possa aiutare di più.

Potresti usare LoadBitmapSample, invece di LoadBitmap.

Quel comando richiede le dimensioni, che tu potresti moltiplicare per la costante Density

upload_2015-1-22_15-4-49.png
 

luciano deri

Active Member
Licensed User
Longtime User
Mi butto, su qualcosa che non conosco affatto, sperando che qualcuno che ci sia già passato ti possa aiutare di più.

Potresti usare LoadBitmapSample, invece di LoadBitmap.

Quel comando richiede le dimensioni, che tu potresti moltiplicare per la costante Density

View attachment 31432
Questa è più comoda perchè semplifica il dimensionamento per evitare che la tagli. dopo vari tentativi il risultato migliore è 35 W ,35H. Comunque è più piccola di quella che creerei a mano. Comunque per adesso non è un problema vitale
 

Attachments

  • screen.png
    screen.png
    500.5 KB · Views: 319

LucaMs

Expert
Licensed User
Longtime User
Io intendevo dire di usare quel codice che hai postato ma sostituire loadbitmap con loadbitmapsample e moltiplicare 72 * density.

Ricordati di eseguire dei test su emulatori con dimensioni e densità diverse, non soltanto su un dispositivo.
 

luciano deri

Active Member
Licensed User
Longtime User
Si ho provato e il risultato è la prima icona. Ho fatto alcuni tentativi ridimensionando con delle costanti e sono arrivato a 35, che pur piccola non è tagliata.
 

seby8181

Member
Licensed User
Longtime User
Salve. Alla primo avvio dell'app voglio creare un l'icona sul desktop principale con la seguente sub (trovata sul forum) chiamata nel create
B4X:
Sub IconaDesktop
    Dim PrefMgr As PreferenceManager
   If PrefMgr.GetBoolean("shortcutinstalled") Then
      Return
   End If
 
   Dim shortcutIntent As Intent
   shortcutIntent.Initialize("", "")
   shortcutIntent.SetComponent("com.multidataitalia.siscoapp/.main") ' Put the app package name here
 
   Dim In As Intent
   In.Initialize("", "")
   In.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
   In.PutExtra("android.intent.extra.shortcut.NAME", "SiscoApp") ' Put you're application name here
   In.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "iconasiscoapp.jpg")) ' If you have the icon file in the assets, you just need any bitmap here. Best is 72x72 pixels because launchers do not scale. You might want to experiment a little bit with size
 
   ' Here starts the real action that all the other posts had missing:
        ' How to get this OUT of your app to the launcher
        ' You need to send a broadcast, that's all :-)
   In.Action = "com.android.launcher.action.INSTALL_SHORTCUT"
 
   Dim p As Phone
   p.SendBroadcastIntent(In)
 
   PrefMgr.SetBoolean("shortcutinstalled", True)
End Sub
Salve, qualcuno avrebbe un esempio completo e funzionante? Ho fatto qualche test e non capisco dove sbaglio... in pratica il programma gira, ma non ottengo nessuna icona sulla home...

Ho anche modificato così il manifest:

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
    
AddApplicationText(
<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="shortcutactivity"
            android:label="Add Shortcut" android:screenOrientation="unspecified">
         <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
      </activity>   
)   
    
    
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
SetActivityAttribute(main, android:theme, @android:style/Theme.Translucent.NoTitleBar)
:(
Grazie!
 

seby8181

Member
Licensed User
Longtime User
Ragazzi, per chi è interessato, la soluzione è quella di modificare il manifest aggiungendo SOLO questa riga di permessi:
B4X:
AddPermission(com.android.launcher.permission.INSTALL_SHORTCUT)

Questo sarà quindi il manifest aggiornato:

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<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.DarkTheme)
'End of default text.
SetActivityAttribute(main, android:theme, @android:style/Theme.Translucent.NoTitleBar)
AddPermission(com.android.launcher.permission.INSTALL_SHORTCUT)

;)
 
Top