摘要 Android系統 小米,三星,索尼手機發送桌面快鍵提醒數字圖標,在Android系統中,眾所周知不支持BadgeNumber,雖然第三方控件BadgeView可以實現應用內的數字提醒,但對於系統的圖標,特別是app的logo圖標很難實現數字標志,即使是繪圖的方式不斷修改,但這種方式天生弊端,實用性很差。
Android系統 小米,三星,索尼手機發送桌面快鍵提醒數字圖標,在Android系統中,眾所周知不支持BadgeNumber,雖然第三方控件BadgeView可以實現應用內的數字提醒,但對於系統的圖標,特別是app的logo圖標很難實現數字標志,即使是繪圖的方式不斷修改,但這種方式天生弊端,實用性很差。但幸運的是,某些ROM廠商提供了私有的API,但也帶來了難度,API的不同意意味着代碼量的增加和兼容性問題更加突出。
我們現在來實現桌面logo或者說icon右上角的圖標,先來看2張圖,第一張來自互聯網,第二張來自個人實踐!(由於實驗條件有限,只能測試小米的(⊙o⊙)…,有興趣的同學測試一下其他的吧)
好了,上代碼
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
|
public
class
MainActivity
extends
Activity {
//必須使用,Activity啟動頁
private
final
static
String lancherActivityClassName = Welcome.
class
.getName();
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.common_listview_layout);
}
@Override
protected
void
onResume() {
super
.onResume();
sendBadgeNumber();
}
private
void
sendBadgeNumber() {
String number =
"35"
;
if
(TextUtils.isEmpty(number)) {
number =
"0"
;
}
else
{
int
numInt = Integer.valueOf(number);
number = String.valueOf(Math.max(
0
, Math.min(numInt,
99
)));
}
if
(Build.MANUFACTURER.equalsIgnoreCase(
"Xiaomi"
)) {
sendToXiaoMi(number);
}
else
if
(Build.MANUFACTURER.equalsIgnoreCase(
"samsung"
)) {
sendToSony(number);
}
else
if
(Build.MANUFACTURER.toLowerCase().contains(
"sony"
)) {
sendToSamsumg(number);
}
else
{
Toast.makeText(
this
,
"Not Support"
, Toast.LENGTH_LONG).show();
}
}
private
void
sendToXiaoMi(String number) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification =
null
;
boolean
isMiUIV6 =
true
;
try
{
NotificationCompat.Builder builder =
new
NotificationCompat.Builder(
this
);
builder.setContentTitle(
"您有"
+number+
"未讀消息"
);
builder.setTicker(
"您有"
+number+
"未讀消息"
);
builder.setAutoCancel(
true
);
builder.setSmallIcon(R.drawable.common_icon_lamp_light_red);
builder.setDefaults(Notification.DEFAULT_LIGHTS);
notification = builder.build();
Class miuiNotificationClass = Class.forName(
"android.app.MiuiNotification"
);
Object miuiNotification = miuiNotificationClass.newInstance();
Field field = miuiNotification.getClass().getDeclaredField(
"messageCount"
);
field.setAccessible(
true
);
field.set(miuiNotification, number);
// 設置信息數
field = notification.getClass().getField(
"extraNotification"
);
field.setAccessible(
true
);
field.set(notification, miuiNotification);
Toast.makeText(
this
,
"Xiaomi=>isSendOk=>1"
, Toast.LENGTH_LONG).show();
}
catch
(Exception e) {
e.printStackTrace();
//miui 6之前的版本
isMiUIV6 =
false
;
Intent localIntent =
new
Intent(
"android.intent.action.APPLICATION_MESSAGE_UPDATE"
);
localIntent.putExtra(
"android.intent.extra.update_application_component_name"
,getPackageName() +
"/"
+ lancherActivityClassName );
localIntent.putExtra(
"android.intent.extra.update_application_message_text"
,number);
sendBroadcast(localIntent);
}
finally
{
if
(notification!=
null
&& isMiUIV6 )
{
//miui6以上版本需要使用通知發送
nm.notify(
101010
, notification);
}
}
}
private
void
sendToSony(String number) {
boolean
isShow =
true
;
if
(
"0"
.equals(number)) {
isShow =
false
;
}
Intent localIntent =
new
Intent();
localIntent.putExtra(
"com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE"
,isShow);
//是否顯示
localIntent.setAction(
"com.sonyericsson.home.action.UPDATE_BADGE"
);
localIntent.putExtra(
"com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME"
,lancherActivityClassName );
//啟動頁
localIntent.putExtra(
"com.sonyericsson.home.intent.extra.badge.MESSAGE"
, number);
//數字
localIntent.putExtra(
"com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME"
,getPackageName());
//包名
sendBroadcast(localIntent);
Toast.makeText(
this
,
"Sony,"
+
"isSendOk"
, Toast.LENGTH_LONG).show();
}
private
void
sendToSamsumg(String number)
{
Intent localIntent =
new
Intent(
"android.intent.action.BADGE_COUNT_UPDATE"
);
localIntent.putExtra(
"badge_count"
, number);
//數字
localIntent.putExtra(
"badge_count_package_name"
, getPackageName());
//包名
localIntent.putExtra(
"badge_count_class_name"
,lancherActivityClassName );
//啟動頁
sendBroadcast(localIntent);
Toast.makeText(
this
,
"Samsumg,"
+
"isSendOk"
, Toast.LENGTH_LONG).show();
}
}
|
注意lancherActivityClassName 必須被配置為 啟動頁 android.intent.category.LAUNCHER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<activity
android:name=
"com.sample.activites.Welcome"
android:configChanges=
"locale|keyboard|screenSize"
android:label=
"@string/app_name"
android:screenOrientation=
"portrait"
>
<intent-filter>
<action android:name=
"android.intent.action.MAIN"
/>
<category android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
<intent-filter>
<action android:name=
"android.intent.action.CREATE_SHORTCUT"
/>
</intent-filter>
</activity>
|
try doing it
在啟動的Activity中、發送未讀消息數目廣播 和 重置/清除未讀消息數目廣播 的調用如下:
// 發送未讀消息數目廣播:count為未讀消息數目(int類型)
BadgeUtil.setBadgeCount(getApplicationContext(), count);
// 發送重置/清除未讀消息數目廣播:
BadgeUtil.resetBadgeCount(getApplicationContext());
資料參考:
http://blog.csdn.net/andylao62/article/details/41794695
http://blog.csdn.net/wx_962464/article/details/37997299
https://github.com/ekinlyw/android-badge
http://www.tuicool.com/articles/JV7vIr
—————————————————————————————————————
如果文章內容對您有幫助, 可以幫 頂 一下,來支持一下哦!
如果您對文章內容有任何疑問或有更好的見解, 歡迎通過留言或發郵件的方式聯系我:
ice_zhengbin@163.com
如需要轉載,請注明出處,謝謝!!
—————————————————————————————————————