欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 实现桌面未读角标

程序员文章站 2022-05-07 11:34:26
在小米 三星  索尼 手机 :图标上显示数字(未读消息数):这部分代码,是从qq5.0.apk中找的。 小米已经测试通过了, 三星和索尼的,由于没有相应的手机...

在小米 三星  索尼 手机 :图标上显示数字(未读消息数):这部分代码,是从qq5.0.apk中找的。

小米已经测试通过了,

三星和索尼的,由于没有相应的手机,没有测试,有的,可能修改一下代码(判断是什么手机的代码),

测试一下,可以在回复一下测试结果,谢谢

1.原生系统(原生的launcher ),只能修改快捷方式,增加和删除都会有toast提示

2.小米 三星  索尼 手机: 自定义的launcher:  发送显示未读消息数的action已经不同了。具体可以去看代码。。。

判断手机的代码:

/***
     * 在应用图标的快捷方式上加数字
     * @param clazz 启动的activity
     * @param isshownum 是否显示数字
     * @param num 显示的数字:整型
     * @param isstroke 是否加上边框
     * 
     */
    public static void addnumshortcut(context context,class<?> clazz,boolean isshownum, string num, boolean isstroke)
     {
         log.e(tag, "manufacturer="+build.manufacturer);
      if (build.manufacturer.equalsignorecase("xiaomi")){
          //小米
       xiaomishortcut(context, clazz, num);

      }else if(build.manufacturer.equalsignorecase("samsung")){
          //三星
          samsungshortcut(context, num);

      }else {//其他原生系统手机
          installrawshortcut(context, mainactivity.class, isshownum, num, isstroke);
      }

     }

下面写好的工具类,上面的方法也在其中。

/***
* 应用的快捷方式工具类
* 
* @author yang
* 
*/
public class appshortcututil {

    private static final string tag = "appshortcututil";

    //默认圆角半径
    private static final int default_corner_radius_dip = 8;
    //默认边框宽度
    private static final int default_stroke_width_dip = 2;
    //边框的颜色
    private static final int default_stroke_color = color.white;
    //中间数字的颜色
    private static final int default_num_color = color.parsecolor("#ccff0000");

    /***
     * 
     * 生成有数字的图片(没有边框)
     * @param context
     * @param icon 图片
     * @param isshownum 是否要绘制数字
     * @param num 数字字符串:整型数字 超过99,显示为"99+"
     * @return
     */
    public static bitmap generatornumicon(context context, bitmap icon, boolean isshownum, string num) {

        displaymetrics dm = context.getresources().getdisplaymetrics();
        //基准屏幕密度
        float basedensity = 1.5f;//240dpi
        float factor = dm.density/basedensity;

        log.e(tag, "density:"+dm.density);
        log.e(tag, "dpi:"+dm.densitydpi);
        log.e(tag, "factor:"+factor);

        // 初始化画布
        int iconsize = (int) context.getresources().getdimension(android.r.dimen.app_icon_size);
        bitmap numicon = bitmap.createbitmap(iconsize, iconsize, config.argb_8888);
        canvas canvas = new canvas(numicon);

        // 拷贝图片
        paint iconpaint = new paint();
        iconpaint.setdither(true);// 防抖动
        iconpaint.setfilterbitmap(true);// 用来对bitmap进行滤波处理,这样,当你选择drawable时,会有抗锯齿的效果
        rect src = new rect(0, 0, icon.getwidth(), icon.getheight());
        rect dst = new rect(0, 0, iconsize, iconsize);
        canvas.drawbitmap(icon, src, dst, iconpaint);

        if(isshownum){

            if(textutils.isempty(num)){
                num = "0";
            }

            if(!textutils.isdigitsonly(num)){
                //非数字
                log.e(tag, "the num is not digit :"+ num);
                num = "0";
            }

            int numint = integer.valueof(num);

            if(numint > 99){//超过99

                num = "99+";

                // 启用抗锯齿和使用设备的文本字体大小
                paint numpaint = new paint(paint.anti_alias_flag | paint.dev_kern_text_flag);
                numpaint.setcolor(color.white);
                numpaint.settextsize(20f*factor);
                numpaint.settypeface(typeface.default_bold);
                int textwidth=(int)numpaint.measuretext(num, 0, num.length());

                log.e(tag, "text width:"+textwidth);

                int circlecenter = (int) (15*factor);//中心坐标
                int circleradius = (int) (13*factor);//圆的半径

                //绘制左边的圆形
                paint leftcirpaint = new paint(paint.anti_alias_flag);
                leftcirpaint.setcolor(color.red);
                canvas.drawcircle(iconsize-circleradius-textwidth+(10*factor), circlecenter, circleradius, leftcirpaint);

                //绘制右边的圆形
                paint rightcirpaint = new paint(paint.anti_alias_flag);
                rightcirpaint.setcolor(color.red);
                canvas.drawcircle(iconsize-circleradius, circlecenter, circleradius, rightcirpaint);

                //绘制中间的距形
                paint rectpaint = new paint(paint.anti_alias_flag);
                rectpaint.setcolor(color.red);
                rectf oval = new rectf(iconsize-circleradius-textwidth+(10*factor), 2*factor, iconsize-circleradius, circleradius*2+2*factor);
                canvas.drawrect(oval, rectpaint);

                //绘制数字
                canvas.drawtext(num, (float)(iconsize-textwidth/2-(24*factor)), 23*factor,    numpaint);

            }else{//<=99

                // 启用抗锯齿和使用设备的文本字体大小
                paint numpaint = new paint(paint.anti_alias_flag | paint.dev_kern_text_flag);
                numpaint.setcolor(color.white);
                numpaint.settextsize(20f*factor);
                numpaint.settypeface(typeface.default_bold);
                int textwidth=(int)numpaint.measuretext(num, 0, num.length());

                log.e(tag, "text width:"+textwidth);

                //绘制外面的圆形
                //paint outcirpaint = new paint(paint.anti_alias_flag);
                //outcirpaint.setcolor(color.white);
                //canvas.drawcircle(iconsize - 15, 15, 15, outcirpaint);

                //绘制内部的圆形
                paint incirpaint = new paint(paint.anti_alias_flag);
                incirpaint.setcolor(color.red);
                canvas.drawcircle(iconsize-15*factor, 15*factor, 15*factor, incirpaint);

                //绘制数字
                canvas.drawtext(num, (float)(iconsize-textwidth/2-15*factor), 22*factor, numpaint);
            }
        }
        return numicon;
    }

    /***
     * 
     * 生成有数字的图片(没有边框)
     * @param context
     * @param icon 图片
     * @param isshownum 是否要绘制数字
     * @param num 数字字符串:整型数字 超过99,显示为"99+"
     * @return
     */
    public static bitmap generatornumicon2(context context, bitmap icon, boolean isshownum, string num) {

        displaymetrics dm = context.getresources().getdisplaymetrics();
        //基准屏幕密度
        float basedensity = 1.5f;//240dpi
        float factor = dm.density/basedensity;

        log.e(tag, "density:"+dm.density);
        log.e(tag, "dpi:"+dm.densitydpi);
        log.e(tag, "factor:"+factor);

        // 初始化画布
        int iconsize = (int) context.getresources().getdimension(android.r.dimen.app_icon_size);
        bitmap numicon = bitmap.createbitmap(iconsize, iconsize, config.argb_8888);
        canvas canvas = new canvas(numicon);

        // 拷贝图片
        paint iconpaint = new paint();
        iconpaint.setdither(true);// 防抖动
        iconpaint.setfilterbitmap(true);// 用来对bitmap进行滤波处理,这样,当你选择drawable时,会有抗锯齿的效果
        rect src = new rect(0, 0, icon.getwidth(), icon.getheight());
        rect dst = new rect(0, 0, iconsize, iconsize);
        canvas.drawbitmap(icon, src, dst, iconpaint);

        if(isshownum){

            if(textutils.isempty(num)){
                num = "0";
            }

            if(!textutils.isdigitsonly(num)){
                //非数字
                log.e(tag, "the num is not digit :"+ num);
                num = "0";
            }

            int numint = integer.valueof(num);

            if(numint > 99){//超过99
                num = "99+";
            }

            //启用抗锯齿和使用设备的文本字体大小
            //测量文本占用的宽度
            paint numpaint = new paint(paint.anti_alias_flag | paint.dev_kern_text_flag);
            numpaint.setcolor(color.white);
            numpaint.settextsize(20f*factor);
            numpaint.settypeface(typeface.default_bold);
            int textwidth=(int)numpaint.measuretext(num, 0, num.length());
            log.e(tag, "text width:"+textwidth);

            /**----------------------------------*
             * todo 绘制圆角矩形背景 start
             *------------------------------------*/
            //圆角矩形背景的宽度
            int backgroundheight = (int) (2*15*factor);
            int backgroundwidth = textwidth>backgroundheight ? (int)(textwidth+10*factor) : backgroundheight;

            canvas.save();//保存状态

            shapedrawable drawable = getdefaultbackground(context);
            drawable.setintrinsicheight(backgroundheight);
            drawable.setintrinsicwidth(backgroundwidth);
            drawable.setbounds(0, 0, backgroundwidth, backgroundheight);
            canvas.translate(iconsize-backgroundwidth, 0);
            drawable.draw(canvas);

            canvas.restore();//重置为之前保存的状态

            /**----------------------------------*
             * todo 绘制圆角矩形背景 end
             *------------------------------------*/

            //绘制数字
            canvas.drawtext(num, (float)(iconsize-(backgroundwidth + textwidth)/2), 22*factor, numpaint);
        }
        return numicon;
    }
    /***
     * 
     * 生成有数字的图片(有边框)
     * @param context
     * @param icon 图片
     * @param isshownum 是否要绘制数字
     * @param num 数字字符串:整型数字 超过99,显示为"99+"
     * @return
     */
    public static bitmap generatornumicon3(context context, bitmap icon, boolean isshownum, string num) {

        displaymetrics dm = context.getresources().getdisplaymetrics();
        //基准屏幕密度
        float basedensity = 1.5f;//240dpi
        float factor = dm.density/basedensity;

        log.e(tag, "density:"+dm.density);
        log.e(tag, "dpi:"+dm.densitydpi);
        log.e(tag, "factor:"+factor);

        // 初始化画布
        int iconsize = (int) context.getresources().getdimension(android.r.dimen.app_icon_size);
        bitmap numicon = bitmap.createbitmap(iconsize, iconsize, config.argb_8888);
        canvas canvas = new canvas(numicon);

        // 拷贝图片
        paint iconpaint = new paint();
        iconpaint.setdither(true);// 防抖动
        iconpaint.setfilterbitmap(true);// 用来对bitmap进行滤波处理,这样,当你选择drawable时,会有抗锯齿的效果
        rect src = new rect(0, 0, icon.getwidth(), icon.getheight());
        rect dst = new rect(0, 0, iconsize, iconsize);
        canvas.drawbitmap(icon, src, dst, iconpaint);

        if(isshownum){

            if(textutils.isempty(num)){
                num = "0";
            }

            if(!textutils.isdigitsonly(num)){
                //非数字
                log.e(tag, "the num is not digit :"+ num);
                num = "0";
            }

            int numint = integer.valueof(num);

            if(numint > 99){//超过99
                num = "99+";
            }

            //启用抗锯齿和使用设备的文本字体大小
            //测量文本占用的宽度
            paint numpaint = new paint(paint.anti_alias_flag | paint.dev_kern_text_flag);
            numpaint.setcolor(color.white);
            numpaint.settextsize(20f*factor);
            numpaint.settypeface(typeface.default_bold);
            int textwidth=(int)numpaint.measuretext(num, 0, num.length());
            log.e(tag, "text width:"+textwidth);

            /**----------------------------------*
             * todo 绘制圆角矩形背景:先画边框,再画内部的圆角矩形 start
             *------------------------------------*/
            //圆角矩形背景的宽度
            int backgroundheight = (int) (2*15*factor);
            int backgroundwidth = textwidth>backgroundheight ? (int)(textwidth+10*factor) : backgroundheight;
            //边框的宽度
            int strokethickness = (int) (2*factor);

            canvas.save();//保存状态

            int strokeheight = backgroundheight + strokethickness*2;
            int strokewidth = textwidth>strokeheight ? (int)(textwidth+ 10*factor + 2*strokethickness) : strokeheight;
            shapedrawable outstroke = getdefaultstrokedrawable(context);
            outstroke.setintrinsicheight(strokeheight);
            outstroke.setintrinsicwidth(strokewidth);
            outstroke.setbounds(0, 0, strokewidth, strokeheight);
            canvas.translate(iconsize-strokewidth-strokethickness, strokethickness);
            outstroke.draw(canvas);

            canvas.restore();//重置为之前保存的状态

            canvas.save();//保存状态

            shapedrawable drawable = getdefaultbackground(context);
            drawable.setintrinsicheight((int) (backgroundheight+2*factor));
            drawable.setintrinsicwidth((int) (backgroundwidth+2*factor));
            drawable.setbounds(0, 0, backgroundwidth, backgroundheight);
            canvas.translate(iconsize-backgroundwidth-2*strokethickness, 2*strokethickness);
            drawable.draw(canvas);

            canvas.restore();//重置为之前保存的状态

            /**----------------------------------*
             * todo 绘制圆角矩形背景 end
             *------------------------------------*/

            //绘制数字
            canvas.drawtext(num, (float)(iconsize-(backgroundwidth + textwidth+4*strokethickness)/2), (22)*factor+2*strokethickness, numpaint);
        }
        return numicon;
    }

    /***
     * 
     * 生成有数字的图片(有边框的)
     * @param context
     * @param icon 图片
     * @param isshownum 是否要绘制数字
     * @param num 数字字符串:整型数字 超过99,显示为"99+"
     * @return
     */
    public static bitmap generatornumicon4(context context, bitmap icon, boolean isshownum, string num) {

        displaymetrics dm = context.getresources().getdisplaymetrics();
        //基准屏幕密度
        float basedensity = 1.5f;//240dpi
        float factor = dm.density/basedensity;

        log.e(tag, "density:"+dm.density);
        log.e(tag, "dpi:"+dm.densitydpi);
        log.e(tag, "factor:"+factor);

        // 初始化画布
        int iconsize = (int) context.getresources().getdimension(android.r.dimen.app_icon_size);
        bitmap numicon = bitmap.createbitmap(iconsize, iconsize, config.argb_8888);
        canvas canvas = new canvas(numicon);

        // 拷贝图片
        paint iconpaint = new paint();
        iconpaint.setdither(true);// 防抖处理
        iconpaint.setfilterbitmap(true);// 用来对bitmap进行滤波处理,这样,当你选择drawable时,会有抗锯齿的效果
        rect src = new rect(0, 0, icon.getwidth(), icon.getheight());
        rect dst = new rect(0, 0, iconsize, iconsize);
        canvas.drawbitmap(icon, src, dst, iconpaint);

        if(isshownum){

            if(textutils.isempty(num)){
                num = "0";
            }

            if(!textutils.isdigitsonly(num)){
                //非数字
                log.e(tag, "the num is not digit :"+ num);
                num = "0";
            }

            int numint = integer.valueof(num);

            if(numint > 99){//超过99
                num = "99+";
            }

            //启用抗锯齿和使用设备的文本字体
            //测量文本占用的宽度
            paint numpaint = new paint(paint.anti_alias_flag | paint.dev_kern_text_flag);
            numpaint.setcolor(color.white);
            numpaint.settextsize(25f*factor);
            numpaint.settypeface(typeface.default_bold);
            int textwidth=(int)numpaint.measuretext(num, 0, num.length());
            log.e(tag, "text width:"+textwidth);

            /**----------------------------------*
             * todo 绘制圆角矩形背景 start
             *------------------------------------*/
            //边框的宽度
            int strokethickness = (int) (default_stroke_width_dip*factor);
            //圆角矩形背景的宽度
            float radiuspx = 15*factor;
            int backgroundheight = (int) (2*(radiuspx+strokethickness));//2*(半径+边框宽度)
            int backgroundwidth = textwidth>backgroundheight ? (int)(textwidth + 10*factor + 2*strokethickness) : backgroundheight;

            canvas.save();//保存状态

            shapedrawable drawable = getdefaultbackground2(context);
            drawable.setintrinsicheight(backgroundheight);
            drawable.setintrinsicwidth(backgroundwidth);
            drawable.setbounds(0, 0, backgroundwidth, backgroundheight);
            canvas.translate(iconsize-backgroundwidth-strokethickness, 2*strokethickness);
            drawable.draw(canvas);

            canvas.restore();//重置为之前保存的状态

            /**----------------------------------*
             * todo 绘制圆角矩形背景 end
             *------------------------------------*/

            //绘制数字
            canvas.drawtext(num, (float)(iconsize-(backgroundwidth + textwidth+2*strokethickness)/2), (float) (25*factor+2.5*strokethickness), numpaint);
        }
        return numicon;
    }

    /***
     * 创建原生系统的快捷方式
     * @param context 
     * @param clazz 启动的activity
     * @param isshownum 是否显示数字
     * @param num 显示的数字:整型
     * @param isstroke 是否加上边框
     */

    public static void installrawshortcut(context context, class<?> clazz, boolean isshownum, string num, boolean isstroke) {
        log.e(tag, "installshortcut....");

        intent shortcutintent = new intent(    "com.android.launcher.action.install_shortcut");
        //名称
        shortcutintent.putextra(intent.extra_shortcut_name,    context.getstring(r.string.app_name));

        // 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加
        shortcutintent.putextra("duplicate", false);

        //点击快捷方式:打开activity
        intent mainintent = new intent(intent.action_main);
        mainintent.addcategory(intent.category_launcher);
        mainintent.setclass(context, clazz);
        shortcutintent.putextra(intent.extra_shortcut_intent, mainintent);

        //快捷方式的图标
        if(isstroke){
            shortcutintent.putextra(intent.extra_shortcut_icon,
                    generatornumicon4(
                            context, 
                            ((bitmapdrawable)context.getresources().getdrawable(r.drawable.ic_launcher)).getbitmap(),
                            isshownum, 
                            num));
        }else{
            shortcutintent.putextra(intent.extra_shortcut_icon,
                    generatornumicon2(
                            context, 
                            ((bitmapdrawable)context.getresources().getdrawable(r.drawable.ic_launcher)).getbitmap(),
                            isshownum, 
                            num));
        }
        context.sendbroadcast(shortcutintent);
    }

    /***
     * 是否已经创建了快捷方式
     * @param context
     * @return
     */
    public static boolean isaddshortcut(context context) {
         log.e(tag, "isaddshortcut....");

      boolean isinstallshortcut = false;
      final contentresolver cr = context.getcontentresolver();

      //todo 注释的代码,在有的手机:修改了rom的系统,不能支持
      /*int versionlevel = android.os.build.version.sdk_int;
            string authority = "com.android.launcher2.settings";
            //2.2以上的系统的文件文件名字是不一样的
            if (versionlevel >= 8) {
              authority = "com.android.launcher2.settings";
            } else {
              authority = "com.android.launcher.settings";
            }*/

        string authority = getauthorityfrompermission(context, "com.android.launcher.permission.read_settings");
        log.e(tag, "authority : " +authority);
        final uri content_uri = uri.parse("content://" + authority
        + "/favorites?notify=true");

        cursor c = cr.query(content_uri,
            new string[] { "title" }, "title=?",
            new string[] { context.getstring(r.string.app_name) }, null);

        if (c != null && c.getcount() > 0) {
          isinstallshortcut = true;
        }

        if(c != null){
            c.close();
        }

        log.e(tag, "isaddshortcut....isinstallshortcut="+isinstallshortcut);

      return isinstallshortcut;
    }

    /**
     * 删除快捷方式 
     * @param context
     * @param clazz
     */
     public static void deleteshortcut(context context, class<?> clazz){
         log.e(tag, "delshortcut....");

         if (build.manufacturer.equalsignorecase("xiaomi")){
            //小米
            //当为""时,不显示数字,相当于隐藏了)
            xiaomishortcut(context, clazz, "");

        }else if(build.manufacturer.equalsignorecase("samsung")){
            //三星
            samsungshortcut(context, "0");

        }else {//其他原生系统手机
            //删除显示数字的快捷方式
            deleterawshortcut(context, clazz);
            //安装不显示数字的快捷方式
            //installrawshortcut(context, clazz, false, "0");
        }
     }

    /***
     * 删除原生系统的快捷方式
     * @param context
     * @param clazz 启动的activity
     */
    public static void deleterawshortcut(context context, class<?> clazz) {
        intent intent = new intent("com.android.launcher.action.uninstall_shortcut");
        //快捷方式的名称
        intent.putextra(intent.extra_shortcut_name, context.getstring(r.string.app_name));

        intent intent2 = new intent(); 
        intent2.setclass(context, clazz); 
        intent2.setaction(intent.action_main); 
        intent2.addcategory(intent.category_launcher); 
        intent.putextra(intent.extra_shortcut_intent,intent2); 

        context.sendbroadcast(intent);
    }

     /***
     * 取得权限相应的认证uri
     * @param context
     * @param permission
     * @return
     */
    public static string getauthorityfrompermission(context context, string permission) {
        if (textutils.isempty(permission)) {
            return null;
        }
        list<packageinfo> packinfos = context.getpackagemanager().getinstalledpackages(packagemanager.get_providers);
        if (packinfos == null) {
            return null;
        }
        for (packageinfo info : packinfos) {
            providerinfo[] providers = info.providers;
            if (providers != null) {
                for (providerinfo provider : providers) {
                    if (permission.equals(provider.readpermission)
                            || permission.equals(provider.writepermission)) {
                        return provider.authority;
                    }
                }
            }
        }
        return null;
    }

    /***
     * 在小米应用图标的快捷方式上加数字<br>
     * 
     * 
     * @param context
     * @param num 显示的数字:大于99,为"99",当为""时,不显示数字,相当于隐藏了)<br><br>
     * 
     * 注意点:
     * context.getpackagename()+"/."+clazz.getsimplename() (这个是启动activity的路径)中的"/."不能缺少
     * 
     */
    public static void xiaomishortcut(context context,class<?> clazz, string num)
     {
        log.e(tag, "xiaomishortcut....");
        intent localintent = new intent("android.intent.action.application_message_update");
        localintent.putextra("android.intent.extra.update_application_component_name", context.getpackagename()+"/."+clazz.getsimplename());
        if(textutils.isempty(num)){
            num = "";
        }else{
          int numint = integer.valueof(num);
          if (numint > 0){
             if (numint > 99){
                num = "99";
           }
          }else{
              num = "0";
          }
        }
        localintent.putextra("android.intent.extra.update_application_message_text", num);
        context.sendbroadcast(localintent);
     }                                            

     /***
     * 索尼手机:应用图标的快捷方式上加数字
     * @param context
     * @param num
     */
    public static void sonyshortcut(context context, string num)
     {
      string activityname = getlaunchactivityname(context);
      if (activityname == null){
       return;
      }
      intent localintent = new intent();
      int numint = integer.valueof(num);
      boolean isshow = true;
      if (numint < 1){
       num = "";
       isshow = false;
      }else if (numint > 99){
          num = "99";
      }
      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", activityname);
      localintent.putextra("com.sonyericsson.home.intent.extra.badge.message", num);
      localintent.putextra("com.sonyericsson.home.intent.extra.badge.package_name", context.getpackagename());
      context.sendbroadcast(localintent);
     }

     /***
     * 三星手机:应用图标的快捷方式上加数字
     * @param context
     * @param num
     */
    public static void samsungshortcut(context context, string num)
     {
        int numint = integer.valueof(num);
      if (numint < 1)
      {
       num = "0";
      }else if (numint > 99){
          num = "99";
      }
         string activityname = getlaunchactivityname(context);
      intent localintent = new intent("android.intent.action.badge_count_update");
      localintent.putextra("badge_count", num);
      localintent.putextra("badge_count_package_name", context.getpackagename());
      localintent.putextra("badge_count_class_name", activityname);
      context.sendbroadcast(localintent);
     }

     /***
     * 在应用图标的快捷方式上加数字
     * @param clazz 启动的activity
     * @param isshownum 是否显示数字
     * @param num 显示的数字:整型
     * @param isstroke 是否加上边框
     * 
     */
    public static void addnumshortcut(context context,class<?> clazz,boolean isshownum, string num, boolean isstroke)
     {
         log.e(tag, "manufacturer="+build.manufacturer);
      if (build.manufacturer.equalsignorecase("xiaomi")){
          //小米
       xiaomishortcut(context, clazz, num);

      }else if(build.manufacturer.equalsignorecase("samsung")){
          //三星
          samsungshortcut(context, num);

      }else {//其他原生系统手机
          installrawshortcut(context, mainactivity.class, isshownum, num, isstroke);
      }

     }

     /***
     * 取得当前应用的启动activity的名称:
     * mainfest.xml中配置的 android:name:"
     * @param context
     * @return
     */
    public static string getlaunchactivityname(context context)
     {
      packagemanager localpackagemanager = context.getpackagemanager();
      intent localintent = new intent("android.intent.action.main");
      localintent.addcategory("android.intent.category.launcher");
      try
      {
       iterator<resolveinfo> localiterator = localpackagemanager.queryintentactivities(localintent, 0).iterator();
       while (localiterator.hasnext())
       {
        resolveinfo localresolveinfo = localiterator.next();
        if (!localresolveinfo.activityinfo.applicationinfo.packagename.equalsignorecase(context.getpackagename()))
         continue;
        string str = localresolveinfo.activityinfo.name;
        return str;
       }
      }
      catch (exception localexception)
      {
       return null;
      }
      return null;
     }

    /***
     * 得到一个默认的背景:圆角矩形<br><br>
     * 使用代码来生成一个背景:相当于用<shape>的xml的背景
     * 
     * @return
     */
    private static shapedrawable getdefaultbackground(context context) {

        //这个是为了应对不同分辨率的手机,屏幕兼容性
        int r = diptopixels(context,default_corner_radius_dip);
        float[] outerr = new float[] {r, r, r, r, r, r, r, r};

        //圆角矩形
        roundrectshape rr = new roundrectshape(outerr, null, null);
        shapedrawable drawable = new shapedrawable(rr);
        drawable.getpaint().setcolor(default_num_color);//设置颜色
        return drawable;

    }
    /***
     * 得到一个默认的背景:圆角矩形<br><br>
     * 使用代码来生成一个背景:相当于用<shape>的xml的背景
     * 
     * @return
     */
    private static shapedrawable getdefaultbackground2(context context) {

        //这个是为了应对不同分辨率的手机,屏幕兼容性
        int r = diptopixels(context,default_corner_radius_dip);
        float[] outerr = new float[] {r, r, r, r, r, r, r, r};
        int distance = diptopixels(context,default_stroke_width_dip);

        //圆角矩形
        roundrectshape rr = new roundrectshape(outerr, null, null);
        customborderdrawable drawable = new customborderdrawable(context,rr);
        drawable.getfillpaint().setcolor(default_num_color);//设置填充颜色
        drawable.getstrokepaint().setcolor(default_stroke_color);//设置边框颜色
        drawable.getstrokepaint().setstrokewidth(distance);//设置边框宽度
        return drawable;

    }

    /***
     * 得到一个默认的背景:圆角矩形<br><br>
     * 使用代码来生成一个背景:相当于用<shape>的xml的背景
     * 
     * @return
     */
    private static shapedrawable getdefaultstrokedrawable(context context) {

        //这个是为了应对不同分辨率的手机,屏幕兼容性
        int r = diptopixels(context, default_corner_radius_dip);
        int distance = diptopixels(context, default_stroke_width_dip);
        float[] outerr = new float[] {r, r, r, r, r, r, r, r};

        //圆角矩形
        roundrectshape rr = new roundrectshape(outerr, null, null);
        shapedrawable drawable = new shapedrawable(rr);
        drawable.getpaint().setstrokewidth(distance);
        drawable.getpaint().setstyle(paint.style.fill);
        drawable.getpaint().setcolor(default_stroke_color);//设置颜色
        return drawable;
    }
    /***
     * dp to px
     * @param dip
     * @return
     */
    public static int diptopixels(context context, int dip) {
        resources r = context.getresources();
        float px = typedvalue.applydimension(typedvalue.complex_unit_dip, dip, r.getdisplaymetrics());
        return (int) px;
    }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!