Android Question how add a badge to app's icon

Ohanian

Active Member
Licensed User
Longtime User
Hello,

i'm trying to add a badge (number or string) on the app's icon.
i've found this example :

B4X:
http://stackoverflow.com/questions/2905542/is-there-a-way-to-add-a-badge-to-an-application-icon-in-android

but i can't make it work in my app,
here's my code :

B4X:
Dim cv As ContentValues
    cv.Initialize
    cv.PutString("package", "com.app.my")
    cv.PutString("class", "com.app.my.Main")
    cv.PutInteger("badgecount", 8)
    Dim u As Uri
    u.Parse("content://com.sec.badge/apps")
    cr.Insert(u, cv)

any suggestion?
 

Ohanian

Active Member
Licensed User
Longtime User
Hi,

as you said the code works only on Samsung devices, is there any way to get the correct content value for other devices?

tanx in advance
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have tried the code above on my Samsung S6 Edge under adding the badge.WRITE permission in the manifest. No result. Did somebody got it to run?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Did somebody got it to run?
Not me. But honestly; i did not tried

BUT i wrapped this today (after reading this thread)

And i tried it with my S6 EDGE running Android 6.0.1

Code used

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim badge As ShortcutBadger
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    badge.Initialize()

    badge.applyCount(17) ' Note this cound to find the icon in the Screenshow
End Sub



Attached you find Example which has the permissions for Samsung phones inside manifest and the libfiles

For anybody reading this thread in future:
The Library and example can be found here.
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Works a treat .. Well Done
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is a complete project code using @DonManfred badger Library, where I insert records into a table in a SQLite database and display the total table record count on the app icon. Therefore, you do not have to open your database to see how many records the database table has. Requires SQL and ShortCutBadger Libs:
B4X:
Sub Process_Globals
    Dim SQL1 As SQL
End Sub

Sub Globals
    Dim MyFolder As String    = "test"
    Dim DBFileName As String = "test.db"
    Dim DBFilePath As String 
    Dim txt As String
    Dim DBTableName As String ="tblOriginal"   
    Dim badger As ShortcutBadger
End Sub

Sub Activity_Create(FirstTime As Boolean)
    badger.Initialize
    If File.ExternalWritable Then 
        DBFilePath = File.DirRootExternal & "/" & MyFolder
    Else 
        DBFilePath = File.DirInternal & "/" & MyFolder
    End If
    File.MakeDir(DBFilePath,"")
   
    If SQL1.IsInitialized =False Then
        SQL1.Initialize(DBFilePath,DBFileName,True)
    End If
   
    txt="DROP TABLE IF EXISTS " & DBTableName
    SQL1.ExecNonQuery(txt)
   
    txt="CREATE TABLE IF NOT EXISTS  " & DBTableName & " (ID INTEGER PRIMARY KEY,COUNTRY TEXT, POPULATION TEXT)"
    SQL1.ExecNonQuery(txt)  
    txt="INSERT INTO " & DBTableName & "  VALUES(?,?,?)"
    SQL1.ExecNonQuery2(txt, Array As Object(Null,"CHINA","1330044000"))
    txt="INSERT INTO " & DBTableName & "  VALUES(?,?,?)"
    SQL1.ExecNonQuery2(txt, Array As Object(Null,"INDIA","1173108018"))
    txt="INSERT INTO " & DBTableName & "  VALUES(?,?,?)"
    SQL1.ExecNonQuery2(txt, Array As Object(Null,"USA","310232863"))
   
    txt="INSERT INTO " & DBTableName & "  VALUES(?,?,?), (?,?,?), (?,?,?)"
    SQL1.ExecNonQuery2(txt, Array As Object(Null,"BELGIUM","10403000", _
    Null,"BOLIVIA","9947412",Null,"IRAQ","29671605") )
   
    txt="INSERT INTO " & DBTableName & "  VALUES(?,?,?), (?,?,?), (?,?,?)"
    SQL1.ExecNonQuery2(txt, Array As Object(Null,"TUNISIA","10589025", _
    Null,"EGYPT","80471869",Null,"FRANCE","64768389") )
   
    Dim MyCount As Int =SQL1.ExecQuerySingleResult("SELECT count(*) from " & DBTableName)   
    badger.applyCount(MyCount)   

End Sub

Suggestions for @DonManfred:
1. You might want to move or have Erel move this whole thread to the Libraries section so it can be noticed.
2. Can the badger count value text size be changed.
Thank you for a great lib.
Mahares
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. You might want to move or have Erel move this whole thread to the Libraries section so it can be noticed.
The library will be released today or tomorrow. So i´ll create a Thread in the Library--section for it leaving this thread as is

2. Can the badger count value text size be changed.
No. Each Desktop-app has it own settings which is used.
Maybe the Desktop-App has a Setting for it; dont know about my Samsung device...
But, if a setting is available, it is not changeable through this lib.

But i know that you can change the size when using Nova-Launcher (has a setting in the options for this). I was using ova Launcher on my Note 3 Device (rooted)
 
Upvote 0

sinuser

Member
Licensed User
Longtime User
hi, i get this error

Logger connesso a: Widefly WF68
--------- beginning of system
--------- beginning of main
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
cDOG6tQk4sw:APA91bE6eQvil3MRYMuXE-m79fkpjlP9QUhXxga0AQhwn_k7g28b9qAP2vMubVDgHSYeNrVH1CTcK__jj3eUiJ7pym8760AdWYeqAURlz8UTC5yz3GzF0EYDAA2bt4BS2-wP4c-iFEFy3XVcJAXb7cPD9UA2djGm9A
true
Class not found: anywheresoftware.b4a.samples.customlistview.customlistview, trying: com.sinergystudio.sinergyup.customlistview
Class not found: anywheresoftware.b4a.samples.customlistview.customlistview, trying: com.sinergystudio.sinergyup.customlistview
Class not found: anywheresoftware.b4a.samples.customlistview.customlistview, trying: com.sinergystudio.sinergyup.customlistview
Error occurred on line: 47 (User)
me.leolin.shortcutbadger.ShortcutBadgeException: Unable to execute badge
at me.leolin.shortcutbadger.ShortcutBadger.applyCountOrThrow(ShortcutBadger.java:102)
at de.donmanfred.ShortcutBadgerwrapper.applyCountOrThrow(ShortcutBadgerwrapper.java:51)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1736)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6257)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: me.leolin.shortcutbadger.ShortcutBadgeException: unable to resolve intent: Intent { act=android.intent.action.BADGE_COUNT_UPDATE (has extras) }
at me.leolin.shortcutbadger.impl.DefaultBadger.executeBadge(DefaultBadger.java:34)
at me.leolin.shortcutbadger.ShortcutBadger.applyCountOrThrow(ShortcutBadger.java:100)
... 20 more
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…