在build.gradle下添加依赖
compile 'me.leolin:ShortcutBadger:1.1.16@aar'
显示
ShortcutBadger.applyCount(context, num);
删除
ShortcutBadger.removeCount(context);
附SharedPreferences工具类
创建Application
public class MyApplication extends Application { private static Context mTotalContext; private static int nScreenWidth; /** * 屏幕高度 */ private static int nScreenHeight; private ListactivityList = new LinkedList (); private static MyApplication instance; @Override public void onCreate() { super.onCreate(); mTotalContext = this.getApplicationContext(); } // 版本号 public static int getVersionCode() { try { return mTotalContext.getPackageManager().getPackageInfo(mTotalContext.getPackageName(), 0).versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return 0; } // 版本名称 public static String getVersionName() { try { return mTotalContext.getPackageManager().getPackageInfo(mTotalContext.getPackageName(), 0).versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return null; } /** * 获取屏幕宽高 */ public static void initScreen(Activity mActivity) { DisplayMetrics dm = new DisplayMetrics(); mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm); MyApplication.setnScreenWidth(dm.widthPixels); MyApplication.setnScreenHeight(dm.heightPixels); // Log.v("lg", "屏幕高和宽。。。" + dm.heightPixels + "....." + dm.widthPixels); } public static Context getTotalContext() { return mTotalContext; } /** * 设置屏幕宽度,在MainActivity,LoadingActivity调用 */ public static void setnScreenWidth(int nScreenWidth) { MyApplication.nScreenWidth = nScreenWidth; if (nScreenWidth != 0) // SpsUtil.editInt(R.string.SCREEN_WIDTH, nScreenWidth); ShareUtil.sharedPint("www", nScreenWidth); } public static int getnScreenWidth() { if (nScreenWidth == 0) // nScreenWidth = SpsUtil.getInt(R.string.SCREEN_WIDTH); nScreenHeight= ShareUtil.getSharedInt("www"); return nScreenWidth; } /** * 设置屏幕高度,在MainActivity,LoadingActivity调用 */ public static void setnScreenHeight(int nScreenHeight) { MyApplication.nScreenHeight = nScreenHeight; if (nScreenHeight != 0) // SpsUtil.editInt(R.string.SCREEN_HEIGHT, nScreenHeight); ShareUtil.sharedPint("hhh", nScreenHeight); } public static int getnScreenHeight() { if (nScreenHeight == 0) // nScreenHeight = SpsUtil.getInt(R.string.SCREEN_HEIGHT); nScreenHeight= ShareUtil.getSharedInt("hhh"); return nScreenHeight; } }
创建ShareUtil
public class ShareUtil { private static SharedPreferences sp; private static Context mContext; // preferences.edit().clear().commit(); 清空数据 private static void initContext() { if (mContext == null){ mContext = MyApplication.getTotalContext(); sp= mContext.getSharedPreferences("lg", 0); } } public static int getSharedInt(String key){ initContext(); int re = 0; re = sp.getInt(key, 0); return re; } public static int getSharedInt(int nResID) { initContext(); return sp.getInt(mContext.getString(nResID), 0); } public static String getSharedString(int nResID) { initContext(); return sp.getString(mContext.getString(nResID), ""); } public static String getSharedString(String key){ initContext(); String s = sp.getString(key,""); return s; } public static long getSharedlong(String key){ initContext(); long s = sp.getLong(key, 0); return s; } public static long getSharedlong(int nResID){ initContext(); long s = sp.getLong(mContext.getString(nResID), 0); return s; } public static void sharedPint(String key,int value){ initContext(); SharedPreferences.Editor editor = sp.edit(); editor.putInt(key, value); editor.commit(); } public static void sharedPint(int nResID, int content) { initContext(); sp.edit().putInt(mContext.getString(nResID), content).commit(); } public static void sharedPstring(int nResID, String strContent) { initContext(); if (TextUtils.isEmpty(strContent)) strContent = ""; sp.edit().putString(mContext.getString(nResID), strContent).commit(); } public static void editLong(int nResID, long content) { initContext(); sp.edit().putLong(mContext.getString(nResID), content).commit(); } public static void editLong(String strKey, long content) { initContext(); sp.edit().putLong(strKey, content).commit(); } public static void sharedPstring(String key,String value){ initContext(); SharedPreferences.Editor editor = sp.edit(); editor.putString(key,value); editor.commit(); } public static void deleP(){ initContext(); sp = mContext.getSharedPreferences("lg",0); sp.edit().clear().commit(); } }
使用
int num = ShareUtil.getSharedInt("num"); num++; ShareUtil.sharedPint("num",num); ShortcutBadger.applyCount(context, num);