安卓app角标
2016-04-25 00:25
218 查看
就一个困惑,android到底能否实现角标?扣扣是怎么实现的??
思路:大家都知道android系统默认是不支持角标的。但是有时候你又可以在很多系统上看到角标,这些系统包括
小米手机的miui 三星手机的TouchWiz 索尼手机;
这些手机的系统应用都可以显示数字角标。这是应为这些系统进行了定制,使用的是自己的launcher. 所以在这些系统上的实现思路就是使用这些手机的私有api来实现显示角标。但是还有一部分手机不支持 实现方法是通过快捷方式,创建一个带角标的应用的快捷方式。需要更新角标的时候就去通过更新快捷方式来实现。
创建快捷方式
public class ShortcutUtil {
public static void createShortCut(Activity act, int iconResId,
int appnameResId) {
// com.android.launcher.permission.INSTALL_SHORTCUT
Intent shortcutintent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutintent.putExtra("duplicate", false);
// 需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
act.getString(appnameResId));
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(
act.getApplicationContext(), iconResId);
sho
4000
rtcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 点击快捷图片,运行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(act.getApplicationContext(), act.getClass()));
// 发送广播
act.sendBroadcast(shortcutintent);
}
}
权限:<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
可在第一次运行程序是提示用户是否创建快捷方式。
下面封装了一个工具类 BadgeUtil.Java 实现了不同手机制造商的未读消息数目广播
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.widget.Toast;
/**
* 应用启动图标未读消息数显示 工具类 (效果如:QQ、微信、未读短信 等应用图标)<br/>
* 依赖于第三方手机厂商(如:小米、三星)的Launcher定制、原生系统不支持该特性<br/>
* 该工具类 支持的设备有 小米、三星、索尼【其中小米、三星亲测有效、索尼未验证】
* @author ice_zhengbin@163.com
*
*/
public class BadgeUtil {
/**
* Set badge count<br/>
* 针对 Samsung / xiaomi / sony 手机有效
* @param context The context of the application package.
* @param count Badge count to be set
*/
public static void setBadgeCount(Context context, int count) {
if (count <= 0) {
count = 0;
} else {
count = Math.max(0, Math.min(count, 99));
}
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
sendToXiaoMi(context, count);
} else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {
sendToSony(context, count);
} else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {
sendToSamsumg(context, count);
} else {
Toast.makeText(context, "Not Support", Toast.LENGTH_LONG).show();
}
}
/**
* 向小米手机发送未读消息数广播
* @param count
*/
private static void sendToXiaoMi(Context context, int count) {
try {
Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
Object miuiNotification = miuiNotificationClass.newInstance();
Field field = miuiNotification.getClass().getDeclaredField("messageCount");
field.setAccessible(true);
// 设置信息数-->这种发送必须是miui 6才行
field.set(miuiNotification, String.valueOf(count == 0 ? "" : count));
} catch (Exception e) {
e.printStackTrace();
// miui 6之前的版本
Intent localIntent = new Intent(
"android.intent.action.APPLICATION_MESSAGE_UPDATE");
localIntent.putExtra(
"android.intent.extra.update_application_component_name",
context.getPackageName() + "/" + getLauncherClassName(context));
localIntent.putExtra(
"android.intent.extra.update_application_message_text"
, String.valueOf(count == 0 ? "" : count));
context.sendBroadcast(localIntent);
}
}
/**
* 向索尼手机发送未读消息数广播<br/>
* 据说:需添加权限:
*<uses-permission
*android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> [未验证]
* @param count
*/
private static void sendToSony(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);
}
/**
* 向三星手机发送未读消息数广播
* @param count
*/
private static void sendToSamsumg(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);
}
/**
* 重置、清除Badge未读显示数<br/>
* @param context
*/
public static void resetBadgeCount(Context context) {
setBadgeCount(context, 0);
}
/**
* Retrieve launcher activity name of the application from the context
*
* @param context The context of the application package.
* @return launcher activity name of this application. From the
* "android:name" attribute.
*/
private static String getLauncherClassName(Context context) {
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
// To limit the components this Intent will resolve to, by setting an
// explicit package name.
intent.setPackage(context.getPackageName());
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// All Application must have 1 Activity at least.
// Launcher activity must be found!
ResolveInfo info = packageManager
.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
// get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER
// if there is no Activity which has filtered by CATEGORY_DEFAULT
if (info == null) {
info = packageManager.resolveActivity(intent, 0);
}
return info.activityInfo.name;
}
}
在启动的Activity中、发送未读消息数目广播 和 重置/清除未读消息数目广播 的调用如下:
// 发送未读消息数目广播:count为未读消息数目(int类型)
BadgeUtil.setBadgeCount(getApplicationContext(), count);
// 发送重置/清除未读消息数目广播:
BadgeUtil.resetBadgeCount(getApplicationContext());
最后献上链接:http://www.eoeandroid.com/thread-542394-1-1.html
点击打开链接 http://blog.csdn.net/jielundewode/article/details/46117379
点击打开链接 http://blog.csdn.net/arui319
http://blog.csdn.net/wx_962464/article/details/37997299
思路:大家都知道android系统默认是不支持角标的。但是有时候你又可以在很多系统上看到角标,这些系统包括
小米手机的miui 三星手机的TouchWiz 索尼手机;
这些手机的系统应用都可以显示数字角标。这是应为这些系统进行了定制,使用的是自己的launcher. 所以在这些系统上的实现思路就是使用这些手机的私有api来实现显示角标。但是还有一部分手机不支持 实现方法是通过快捷方式,创建一个带角标的应用的快捷方式。需要更新角标的时候就去通过更新快捷方式来实现。
创建快捷方式
public class ShortcutUtil {
public static void createShortCut(Activity act, int iconResId,
int appnameResId) {
// com.android.launcher.permission.INSTALL_SHORTCUT
Intent shortcutintent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutintent.putExtra("duplicate", false);
// 需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
act.getString(appnameResId));
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(
act.getApplicationContext(), iconResId);
sho
4000
rtcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 点击快捷图片,运行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(act.getApplicationContext(), act.getClass()));
// 发送广播
act.sendBroadcast(shortcutintent);
}
}
权限:<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
可在第一次运行程序是提示用户是否创建快捷方式。
下面封装了一个工具类 BadgeUtil.Java 实现了不同手机制造商的未读消息数目广播
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.widget.Toast;
/**
* 应用启动图标未读消息数显示 工具类 (效果如:QQ、微信、未读短信 等应用图标)<br/>
* 依赖于第三方手机厂商(如:小米、三星)的Launcher定制、原生系统不支持该特性<br/>
* 该工具类 支持的设备有 小米、三星、索尼【其中小米、三星亲测有效、索尼未验证】
* @author ice_zhengbin@163.com
*
*/
public class BadgeUtil {
/**
* Set badge count<br/>
* 针对 Samsung / xiaomi / sony 手机有效
* @param context The context of the application package.
* @param count Badge count to be set
*/
public static void setBadgeCount(Context context, int count) {
if (count <= 0) {
count = 0;
} else {
count = Math.max(0, Math.min(count, 99));
}
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
sendToXiaoMi(context, count);
} else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {
sendToSony(context, count);
} else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {
sendToSamsumg(context, count);
} else {
Toast.makeText(context, "Not Support", Toast.LENGTH_LONG).show();
}
}
/**
* 向小米手机发送未读消息数广播
* @param count
*/
private static void sendToXiaoMi(Context context, int count) {
try {
Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
Object miuiNotification = miuiNotificationClass.newInstance();
Field field = miuiNotification.getClass().getDeclaredField("messageCount");
field.setAccessible(true);
// 设置信息数-->这种发送必须是miui 6才行
field.set(miuiNotification, String.valueOf(count == 0 ? "" : count));
} catch (Exception e) {
e.printStackTrace();
// miui 6之前的版本
Intent localIntent = new Intent(
"android.intent.action.APPLICATION_MESSAGE_UPDATE");
localIntent.putExtra(
"android.intent.extra.update_application_component_name",
context.getPackageName() + "/" + getLauncherClassName(context));
localIntent.putExtra(
"android.intent.extra.update_application_message_text"
, String.valueOf(count == 0 ? "" : count));
context.sendBroadcast(localIntent);
}
}
/**
* 向索尼手机发送未读消息数广播<br/>
* 据说:需添加权限:
*<uses-permission
*android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> [未验证]
* @param count
*/
private static void sendToSony(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);
}
/**
* 向三星手机发送未读消息数广播
* @param count
*/
private static void sendToSamsumg(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);
}
/**
* 重置、清除Badge未读显示数<br/>
* @param context
*/
public static void resetBadgeCount(Context context) {
setBadgeCount(context, 0);
}
/**
* Retrieve launcher activity name of the application from the context
*
* @param context The context of the application package.
* @return launcher activity name of this application. From the
* "android:name" attribute.
*/
private static String getLauncherClassName(Context context) {
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
// To limit the components this Intent will resolve to, by setting an
// explicit package name.
intent.setPackage(context.getPackageName());
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// All Application must have 1 Activity at least.
// Launcher activity must be found!
ResolveInfo info = packageManager
.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
// get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER
// if there is no Activity which has filtered by CATEGORY_DEFAULT
if (info == null) {
info = packageManager.resolveActivity(intent, 0);
}
return info.activityInfo.name;
}
}
在启动的Activity中、发送未读消息数目广播 和 重置/清除未读消息数目广播 的调用如下:
// 发送未读消息数目广播:count为未读消息数目(int类型)
BadgeUtil.setBadgeCount(getApplicationContext(), count);
// 发送重置/清除未读消息数目广播:
BadgeUtil.resetBadgeCount(getApplicationContext());
最后献上链接:http://www.eoeandroid.com/thread-542394-1-1.html
点击打开链接 http://blog.csdn.net/jielundewode/article/details/46117379
点击打开链接 http://blog.csdn.net/arui319
http://blog.csdn.net/wx_962464/article/details/37997299
相关文章推荐
- android studio学习---怎么创建一个新的module并且再次运行起来(在当前的project里面)
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
- iOS开发系列--视图切换
- iOS学习总结之ARC和非ARC的单例模式实现
- iOS开发需要哪些图片?
- iOS开发系列--无限循环的图片浏览器
- Android 常用工具类之 DimenUtil
- 【Unity2D游戏实战 之 2D滚屏射击】1.背景滚动 (一)
- iOS开发系列—Objective-C之Foundation框架
- 模仿J2EE的session机制的App后端会话信息管理实例
- android studio学习----添加项目依赖包补充---添加github上的开源项目为库
- 不靠爹 非学霸 80后兼职做出4亿用户APP
- iOS设置声音和震动
- 微信公众平台的接口url
- json字符串转换为JSONObject和JSONArray .
- 学习android前配置
- 微信开发
- android笔记(1)
- 微信文章排版
- 微信文章如何获得上万次转载?