The most complete angle standard adaptation method for Android Applications

0 public method

There are many common codes in the following implementation codes. We extract some common methods. The codes are as follows:

 1.      private static String getLauncherClassName(Context context) {
    
2.          ComponentName launchComponent = getLauncherComponentName(context);
    
3.          if (launchComponent == null) {
    
4.              return "";
    
5.          } else {
    
6.              return launchComponent.getClassName();
    
7.          }
    
8.      }
    

10.      private static ComponentName getLauncherComponentName(Context context) {
    
11.          Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context
    
12.                  .getPackageName());
    
13.          if (launchIntent != null) {
    
14.              return launchIntent.getComponent();
    
15.          } else {
    
16.              return null;
    
17.          }
    
18.      }

1 Xiaomi (support)

In MIUI6 and above versions, Xiaomi sets the corner sign by sending a notice. When the app sends a notification to the notification bar (the notification does not have a progress bar and can be deleted by the user), the desktop APP icon corner will display 1. At this time, the number of corner markers displayed by the app corresponds to the number of notifications sent by the app in the notification bar, that is, the number of corner markers will be displayed according to the number of notifications sent to the notification bar.

The number of messages per notification can be defined through the reflection mechanism. The number of applied subscripts is the sum of the number of notifications defined for each notification

We sent two notifications, one for 5 messages, one for 10 messages, and the application corner shows 15 messages. At this time, if the user clicks or removes the notification of 5 messages, the application corner will become 10.

In addition, the corner sign will be displayed only when the APP receives the notice in the background, but will not be displayed when the APP is in the foreground. After the APP is killed, the notice and corner sign will disappear.

Implementation code:

 1.      public static boolean setNotificationBadge(int count, Context context) {
    
2.          NotificationManager notificationManager = (NotificationManager) context.getSystemService
    
3.                  (Context.NOTIFICATION_SERVICE);
    
4.          if (notificationManager == null) {
    
5.              return false;
    
6.          }
    
7.          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
8.              // NotificationChannel is required to add corner markers after 8.0
    
9.              NotificationChannel channel = new NotificationChannel("badge", "badge",
    
10.                      NotificationManager.IMPORTANCE_DEFAULT);
    
11.              channel.setShowBadge(true);
    
12.              notificationManager.createNotificationChannel(channel);
    
13.          }
    
14.          Intent intent = new Intent(context, MainActivity.class);
    
15.          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    
16.          Notification notification = new NotificationCompat.Builder(context, "badge")
    
17.                  .setContentTitle("Application angle mark")
    
18.                  .setContentText("You have" + count + "Unread messages")
    
19.                  .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap
    
20.                          .ic_launcher))
    
21.                  .setSmallIcon(R.mipmap.ic_launcher_round)
    
22.                  .setAutoCancel(true)
    
23.                  .setContentIntent(pendingIntent)
    
24.                  .setChannelId("badge")
    
25.                  .setNumber(count)
    
26.                  .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL).build();
    
27.          // millet
    
28.          try {
    
29.              Field field = notification.getClass().getDeclaredField("extraNotification");
    
30.              Object extraNotification = field.get(notification);
    
31.              Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int
    
32.                      .class);
    
33.              method.invoke(extraNotification, count);
    
34.          } catch (Exception e) {
    
35.              e.printStackTrace();
    
36.          }
    
37.          notificationManager.notify(notificationId++, notification);
    
38.          return true;
    
39.      }

Test results:

Corner markers can be set by notice for Xiaomi 6, Xiaomi 5S, Xiaomi MIX, Xiaomi 5X, Xiaomi 4, Hongmi Note 4X and Xiaomi 2S.

2 Huawei (support)

The application corner can be set directly through the ContentResolver method, and the application can still be displayed in the foreground and after being killed.

Implementation code:

 1.      private static boolean setHuaweiBadge(int count, Context context) {
    
2.          try {
    
3.              String launchClassName = getLauncherClassName(context);
    
4.              if (TextUtils.isEmpty(launchClassName)) {
    
5.                  return false;
    
6.              }
    
7.              Bundle bundle = new Bundle();
    
8.              bundle.putString("package", context.getPackageName());
    
9.              bundle.putString("class", launchClassName);
    
10.              bundle.putInt("badgenumber", count);
    
11.              context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher" +
    
12.                      ".settings/badge/"), "change_badge", null, bundle);
    
13.              return true;
    
14.          } catch (Exception e) {
    
15.              e.printStackTrace();
    
16.              return false;
    
17.          }
    
18.      }

Test results:

Glory 8, Huawei P10, Huawei Mate10 and Huawei maimang (MLA-AL00) can be set directly.

3 OPPO (not supported)

There are two methods to set up the old OPPO mobile phone. There is no tester to test. I don't know whether it is feasible. The implementation code is as follows:

 1.      private static boolean setOPPOBadge(int count, Context context) {
    
2.          try {
    
3.              Bundle extras = new Bundle();
    
4.              extras.putInt("app_badge_count", count);
    
5.              context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"),
    
6.                      "setAppBadgeCount", String.valueOf(count), extras);
    
7.              return true;
    
8.          } catch (Exception e) {
    
9.              e.printStackTrace();
    
10.              return false;
    
11.          }
    
12.      }
    
13.      private static boolean setOPPOBadge2(int count, Context context) {
    
14.          try {
    
15.              Intent intent = new Intent("com.oppo.unsettledevent");
    
16.              intent.putExtra("packageName", context.getPackageName());
    
17.              intent.putExtra("number", count);
    
18.              intent.putExtra("upgradeNumber", count);
    
19.              PackageManager packageManager = context.getPackageManager();
    
20.              List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
    
21.              if (receivers != null && receivers.size() > 0) {
    
22.                  context.sendBroadcast(intent);
    
23.              } else {
    
24.                  Bundle extras = new Bundle();
    
25.                  extras.putInt("app_badge_count", count);
    
26.                  context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"),
    
27.                          "setAppBadgeCount", null, extras);
    
28.              }
    
29.              return true;
    
30.          } catch (Exception e) {
    
31.              e.printStackTrace();
    
32.              return false;
    
33.          }
    
34.      }

The new OPPO only supports built-in applications, wechat and QQ display corner signs. If you want to use the corner sign function, you must submit an application and open it only after passing the review. The specific review standards given by the official are as follows:

Apply for corner sign access rules (the application must be adapted to OPPO mobile phone to ensure that the corner sign function test passes)

a) System application;

b) Three party instant messaging applications with the highest number of users in all regions at home and abroad, and only instant messaging notifications (such as QQ, wechat, facebook, line) are allowed to be displayed;

c) OPPO company's internal fee commercialization and operation of office type instant messaging applications (such as Teamtalk);

d) Mail applications at home and abroad (each region belongs to the first tier of users).

4 vivo (not supported)

Implementation code of old vivo mobile phone:

 1.      private static boolean setVivoBadge(int count, Context context) {
    
2.          try {
    
3.              String launcherClassName = getLauncherClassName(context);
    
4.              if (TextUtils.isEmpty(launcherClassName)) {
    
5.                  return false;
    
6.              }
    
7.              Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
    
8.              intent.putExtra("packageName", context.getPackageName());
    
9.              intent.putExtra("className", launcherClassName);
    
10.              intent.putExtra("notificationNum", count);
    
11.              context.sendBroadcast(intent);
    
12.              return true;
    
13.          } catch (Exception e) {
    
14.              e.printStackTrace();
    
15.              return false;
    
16.          }
    
17.      }

5 Samsung (support)

The application corner mark can be set directly through the broadcast mechanism, and the application can still be displayed in the foreground and after being killed.

Implementation code:

 1.      private static boolean setSamsungBadge(int count, Context context) {
    
2.          try {
    
3.              String launcherClassName = getLauncherClassName(context);
    
4.              if (TextUtils.isEmpty(launcherClassName)) {
    
5.                  return false;
    
6.              }
    
7.              Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
    
8.              intent.putExtra("badge_count", count);
    
9.              intent.putExtra("badge_count_package_name", context.getPackageName());
    
10.              intent.putExtra("badge_count_class_name", launcherClassName);
    
11.              context.sendBroadcast(intent);
    
12.              return true;
    
13.          } catch (Exception e) {
    
14.              e.printStackTrace();
    
15.              return false;
    
16.          }
    
17.      }

Test results:

Samsung S9 +: it needs to be combined with the notification method to display the corner mark.

Samsung Galaxy S6 edge: can be set directly.

6 Meizu (not supported)

No official support.

7 360 (not supported)

At present, it may only support system applications, wechat and QQ, not Alipay, and the relevant documents for setting corner markers are not found.

8 hammer (not supported)

At present, only system applications and wechat are supported, and QQ is not even supported.

9 Nubia (not supported)

At present, only system applications are supported.

10 Jinli (not supported)

No related documents were found.

11 LETV (support, unable to customize the number)

Only corner markers can be set through notification, and the number of corner markers is the number of notifications received. The number of corner markers cannot be customized.

Test results:

Le 2 Pro: can be set by notification.

12 Lenovo ZUK (support)

Implementation code:

 1.      private static boolean setZukBadge(int count, Context context) {
    
2.          try {
    
3.              Bundle extra = new Bundle();
    
4.              ArrayList<String> ids = new ArrayList<>();
    
5.              // The shortcut id is passed in the form of a list, and multiple shortcut IDS can be added
    
6.  //        ids.add("custom_id_1");
    
7.  //        ids.add("custom_id_2");
    
8.              extra.putStringArrayList("app_shortcut_custom_id", ids);
    
9.              extra.putInt("app_badge_count", count);
    
10.              Uri contentUri = Uri.parse("content://com.android.badge/badge");
    
11.              Bundle bundle = context.getContentResolver().call(contentUri, "setAppBadgeCount", null,
    
12.                      extra);
    
13.              return bundle != null;
    
14.          } catch (Exception e) {
    
15.              e.printStackTrace();
    
16.              return false;
    
17.          }
    
18.      }

No tester test

13 ZTE (not supported)

At present, only system applications and wechat are supported, and QQ is not even supported.

14 HTC (support)

Corner markers can be set directly through the broadcast mechanism.

Implementation code:

 1.      private static boolean setHTCBadge(int count, Context context) {
    
2.          try {
    
3.              ComponentName launcherComponentName = getLauncherComponentName(context);
    
4.              if (launcherComponentName == null) {
    
5.                  return false;
    
6.              }
    

8.              Intent intent1 = new Intent("com.htc.launcher.action.SET_NOTIFICATION");
    
9.              intent1.putExtra("com.htc.launcher.extra.COMPONENT", launcherComponentName
    
10.                      .flattenToShortString());
    
11.              intent1.putExtra("com.htc.launcher.extra.COUNT", count);
    
12.              context.sendBroadcast(intent1);
    

14.              Intent intent2 = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");
    
15.              intent2.putExtra("packagename", launcherComponentName.getPackageName());
    
16.              intent2.putExtra("count", count);
    
17.              context.sendBroadcast(intent2);
    

19.              return true;
    
20.          } catch (Exception e) {
    
21.              e.printStackTrace();
    
22.              return false;
    
23.          }
    
24.      }

Test results:

HTC D820u and HTC M8St can be set directly

15 Nokia (support)

The number of corner markers can be set through notification, which is similar to Xiaomi. The difference is that as long as the notification in the notification column always exists, the number always exists, and the application can still be set through notification in the foreground.

Implementation code:

The implementation code is the same as that of Xiaomi.

Test results:

Nokia 7: can be set via notification.

16 Sony (support)

Implementation code:

 1.      private static boolean setSonyBadge(int count, Context context) {
    
2.          String launcherClassName = getLauncherClassName(context);
    
3.          if (TextUtils.isEmpty(launcherClassName)) {
    
4.              return false;
    
5.          }
    
6.          try {
    
7.              //Official method
    
8.              ContentValues contentValues = new ContentValues();
    
9.              contentValues.put("badge_count", count);
    
10.              contentValues.put("package_name", context.getPackageName());
    
11.              contentValues.put("activity_name", launcherClassName);
    
12.              SonyAsyncQueryHandler asyncQueryHandler = new SonyAsyncQueryHandler(context
    
13.                      .getContentResolver());
    
14.              asyncQueryHandler.startInsert(0, null, Uri.parse("content://com.sonymobile.home" +
    
15.                      ".resourceprovider/badge"), contentValues);
    
16.              return true;
    
17.          } catch (Exception e) {
    
18.              try {
    
19.                  //Most usage methods on the Internet
    
20.                  Intent intent = new Intent("com.sonyericsson.home.action.UPDATE_BADGE");
    
21.                  intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", count > 0);
    
22.                  intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",
    
23.                          launcherClassName);
    
24.                  intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String
    
25.                          .valueOf(count));
    
26.                  intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context
    
27.                          .getPackageName());
    
28.                  context.sendBroadcast(intent);
    
29.                  return true;
    
30.              } catch (Exception e1) {
    
31.                  e1.printStackTrace();
    
32.                  return false;
    
33.              }
    
34.          }
    
35.      }
    

37.      static class SonyAsyncQueryHandler extends AsyncQueryHandler {
    
38.          SonyAsyncQueryHandler(ContentResolver cr) {
    
39.              super(cr);
    
40.          }
    
41.      }

No tester test

17 native Android (partially supported, unable to display the number directly)

For Android 8.0 and later versions, Google's official API supports setting application corner markers by sending system notifications, but it does not support displaying the number, but a small point, as shown in the figure below.

When sending a notification, you can set the number of messages. Some mobile phones will display the sum of the number of all notification settings when long pressing the icon. As shown in the above figure, in this example, we sent two notifications. One set the number of messages to 88 and the other to 12, so the total is displayed as 100.

Implementation code:

The implementation code is the same as that of Xiaomi.

Test results:

Pixel 2 XL (Android 9.0): supports notification settings.

Nexus 6P (Android 8.1.0), Nexus 5X (Android 8.1.0): not supported.

One plus OnePlus3 (Android 8.0.0): support notification settings, but long press the icon will not display the number.

Tags: Android

Posted by eliezer on Sun, 15 May 2022 13:16:51 +0300