android badge,Android应用图标上的小红点Badge实现

原文:http://blog.youkuaiyun.com/qq_33689414/article/details/53914439

Android应用图标上的小红点Badge实现

为了增加APP的日活,在Android中有许多的应用仿苹果的在应用图标上显示小红点。当然有着一些手机ROM对小红点的支持,比如小米,三星等。

我们下面看下如何实现:

MainActivity:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58public class MainActivity extends AppCompatActivity {

private Handler handler = new Handler();

private Runnable runnable = new MyRunnable();

public class MyRunnable implements Runnable {

@Override

public void run() {

handler.postDelayed(runnable, 5000);

BadgeUtil.setBadgeCount(getApplicationContext(), getCount(), R.mipmap.ic_launcher);

}

}

private int getCount() {

return new Random().nextInt(100);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button start = (Button) findViewById(R.id.start);

Button stop = (Button) findViewById(R.id.stop);

start.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (handler == null) {

handler = new Handler();

}

if (runnable == null) {

runnable = new MyRunnable();

}

handler.post(runnable);

}

});

stop.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (handler != null) {

BadgeUtil.resetBadgeCount(getApplicationContext(), R.mipmap.ic_launcher);

handler.removeCallbacksAndMessages(null);

runnable = null;

handler = null;

}

}

});

}

}

BadgeUtil:目前支持小米,三星,LG,索尼,HTC等手机

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145public final class BadgeUtil {

private BadgeUtil() throws InstantiationException {

throw new InstantiationException("This class is not for instantiation");

}

/** * 设置Badge 目前支持Launcher */

public static void setBadgeCount(Context context, int count, int iconResId) {

if (count <= 0) {

count = 0;

} else {

count = Math.max(0, Math.min(count, 99));

}

if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {

setBadgeOfMIUI(context, count, iconResId);

} else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {

setBadgeOfSony(context, count);

} else if (Build.MANUFACTURER.toLowerCase().contains("samsung") ||

Build.MANUFACTURER.toLowerCase().contains("lg")) {

setBadgeOfSumsung(context, count);

} else if (Build.MANUFACTURER.toLowerCase().contains("htc")) {

setBadgeOfHTC(context, count);

} else if (Build.MANUFACTURER.toLowerCase().contains("nova")) {

setBadgeOfNova(context, count);

} else {

Toast.makeText(context, "Not Found Support Launcher", Toast.LENGTH_LONG).show();

}

}

/** * 设置MIUI的Badge */

private static void setBadgeOfMIUI(Context context, int count, int iconResId) {

NotificationManager mNotificationManager = (NotificationManager) context

.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setContentTitle("标题").setContentText("消息正文").setSmallIcon(iconResId);

Notification notification = builder.build();

try {

Field field = notification.getClass().getDeclaredField("extraNotification");

Object extraNotification = field.get(notification);

Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);

method.invoke(extraNotification, count);

} catch (Exception e) {

e.printStackTrace();

}

mNotificationManager.notify(0, notification);

}

/** * 设置索尼的Badge * 需添加权限: */

private static void setBadgeOfSony(Context context, int count) {

String launcherClassName = getLauncherClassName(context);

if (launcherClassName == null) {

return;

}

boolean isShow = true;

if (count == 0) {

isShow = false;

}

Intent localIntent = new Intent();

localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");

localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isShow);//是否显示

localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);//启动页

localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));//数字

localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名

context.sendBroadcast(localIntent);

}

/** * 设置三星的Badge\设置LG的Badge */

private static void setBadgeOfSumsung(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);

}

/** * 设置HTC的Badge */

private static void setBadgeOfHTC(Context context, int count) {

Intent intentNotification = new Intent("com.htc.launcher.action.SET_NOTIFICATION");

ComponentName localComponentName = new ComponentName(context.getPackageName(), getLauncherClassName(context));

intentNotification.putExtra("com.htc.launcher.extra.COMPONENT", localComponentName.flattenToShortString());

intentNotification.putExtra("com.htc.launcher.extra.COUNT", count);

context.sendBroadcast(intentNotification);

Intent intentShortcut = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");

intentShortcut.putExtra("packagename", context.getPackageName());

intentShortcut.putExtra("count", count);

context.sendBroadcast(intentShortcut);

}

/** * 设置Nova的Badge */

private static void setBadgeOfNova(Context context, int count) {

ContentValues contentValues = new ContentValues();

contentValues.put("tag", context.getPackageName() + "/" + getLauncherClassName(context));

contentValues.put("count", count);

context.getContentResolver().insert(Uri.parse("content://com.teslacoilsw.notifier/unread_count"),

contentValues);

}

public static void setBadgeOfMadMode(Context context, int count, String packageName, String className) {

Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");

intent.putExtra("badge_count", count);

intent.putExtra("badge_count_package_name", packageName);

intent.putExtra("badge_count_class_name", className);

context.sendBroadcast(intent);

}

/** * 重置Badge */

public static void resetBadgeCount(Context context, int iconResId) {

setBadgeCount(context, 0, iconResId);

}

public static String getLauncherClassName(Context context) {

PackageManager packageManager = context.getPackageManager();

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.setPackage(context.getPackageName());

intent.addCategory(Intent.CATEGORY_LAUNCHER);

ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

if (info == null) {

info = packageManager.resolveActivity(intent, 0);

}

return info.activityInfo.name;

}

}

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值