简体   繁体   English

在图标启动器android中显示抗辩通知

[英]display counter notification in icon launcher android

In my android application i want to display the count icon badge. 在我的Android应用程序中,我想显示计数图标徽章。 i'm using the code from enter link description here .when i test it i always get this error. 我在这里使用输入链接描述中的代码。当我测试它时,我总是会收到此错误。 unable the execute badge.shortcutbadger is currently not support the home launcher package"com.android.launcher" . unable the execute badge.shortcutbadger is currently not support the home launcher package"com.android.launcher" when i debug it i find that the application dosn't get correctly the package name. 当我调试它时,我发现该应用程序没有正确获得软件包名称。

 Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
    String currentHomePackage = resolveInfo.activityInfo.packageName;
   //String currentHomePackage = "me.leolin.shortcutbadger.example.MainActivity";
    TextView textViewHomePackage = (TextView) findViewById(R.id.textViewHomePackage);
    textViewHomePackage.setText("launcher:" + currentHomePackage);

so i put it manually but i always get the same message error with the correct package name. 所以我手动把它放了,但是我总是用正确的包名得到同样的消息错误。

The library you're using only works with the default Samsung, HTC, LG, Sony and other launchers, but not the official Android launcher ( com.android.launcher ). 您使用的库仅适用于默认的Samsung,HTC,LG,Sony和其他启动器,但不适用于官方的Android启动器( com.android.launcher )。

That's probably why you're having issues testing your app. 这可能就是为什么您在测试应用程序时遇到问题。

You can set the badge count for the launcher icon using the following code, but this feature is not supported for all the devices it supported on the devices like Samsung, HTC etc... 您可以使用以下代码来为启动器图标设置徽章计数,但是并非三星,HTC等设备支持的所有设备都支持此功能。

 public static void setBadge(Context context, int count) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }
        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);
    }

public static String getLauncherClassName(Context context) {

    PackageManager pm = context.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : resolveInfos) {
        String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
        if (pkgName.equalsIgnoreCase(context.getPackageName())) {
            String className = resolveInfo.activityInfo.name;
            return className;
        }
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM