I want to put a badge on the icon displayed on HOME like the iPhone.
I dealt with it at work, so I will write it as a memo.
There is a method, but please note that it is not applicable to all HOME apps and can only be displayed on some HOME apps.
After checking, it worked with the following HOME app.
--Galaxy TouchWiz
Other HOME apps have not been verified yet, so I will verify them later. At work, I also displayed it on Xperia Home, but in that case the implementation is a little different, so I will add it later. ..
Then it is an implementation. It's a hassle, so I'll just paste the roughly created methods.
Please refer to here. (Rather, it is almost the same.)
/**
*Clear the badge on the icon.<br>
** Set 0
*
* @param context context
*/
public static void clearIconBadge(final Context context) {
setIconBadge(context, 0);
}
/**
*Put a badge on the icon.
*
* @param context context
* @param count Badge number to display
*/
public static void setIconBadge(final Context context, final int count) {
if (context == null) {
return;
}
final String launcherClassName = getLauncherClassName(context);
if (TextUtils.isEmpty(launcherClassName)) {
return;
}
final Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", launcherClassName);
context.sendBroadcast(intent);
}
/**
*Get the class name of the running app.
*
* @param context Context.
* @return Class name of the running app<br>
*Null if it cannot be obtained normally.
*/
@Nullable
private static String getLauncherClassName(@NonNull final Context context) {
final PackageManager pm = context.getPackageManager();
final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(intent, 0);
for (final ResolveInfo resolveInfo : resolveInfoList) {
if (resolveInfo == null) {
continue;
}
final String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
if (pkgName.equalsIgnoreCase(context.getPackageName())) {
return resolveInfo.activityInfo.name;
}
}
return null;
}
Call setIconBadge () when you want the badge to appear. Also, if you want to erase the badge, you can set 0, so if it is the code written in ↑ It's OK if you call clearIconBadge ().
I will post it for the Kotlin language as well. Paste the created Utility as it is.
class Utility {
companion object {
/**
*Clear the icon badge.<br>
** Set 0
*
* @param context context
*/
fun clearIconBadge(context: Context) {
setIconBadge(context, 0)
}
/**
*Put a badge on the icon.
* @param context context
*
* @param count Badge number to display
*/
fun setIconBadge(context: Context?, count: Int) {
if (context == null) {
return
}
val launcherClassName = getLauncherClassName(context)
if (TextUtils.isEmpty(launcherClassName)) {
return
}
val intent = Intent("android.intent.action.BADGE_COUNT_UPDATE")
intent.putExtra("badge_count", count)
intent.putExtra("badge_count_package_name", context.packageName)
intent.putExtra("badge_count_class_name", launcherClassName)
context.sendBroadcast(intent)
}
/**
*Get the class name of the running app.
*
* @param context Context.
* @return Class name of the running app<br></br>
* *Null if it cannot be obtained normally.
*/
private fun getLauncherClassName(context: Context): String? {
val pm = context.packageManager
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val resolveInfoList = pm.queryIntentActivities(intent, 0)
for (resolveInfo in resolveInfoList) {
if (resolveInfo == null) {
continue
}
val pkgName = resolveInfo.activityInfo.applicationInfo.packageName
if (pkgName.equals(context.packageName, ignoreCase = true)) {
return resolveInfo.activityInfo.name
}
}
return null
}
}
}
Recommended Posts