Android 桌面生成快捷方式

Android生成桌面快捷方式的几种方法:

 

//------------以下为动态替换桌面应用Icon的一种解决方案-------------------


    // 1.获取本地目录图片的Bitmap ;根据Bitmap绘制新的canvas画布Jicanvas画布上添加文字信息;最终获得一个带有canvas的Bitmap:NewIcon

    public Bitmap getBitmap() {
        //获取本地bitmap
        Bitmap bitmap = BitmapFactory.decodeFile("/scard/1.png");
        Bitmap bitmap2 = BitmapFactory.decodeFile("/scard/2.png");
        //根据Bitmap绘制新的Canvas画布
        Bitmap NewIcon = Bitmap.createBitmap(android.R.dimen.app_icon_size, android.R.dimen.app_icon_size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(NewIcon);
        int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
        Paint iconPaint = new Paint();
        Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        Rect dst = new Rect(0, 0, iconSize, iconSize);

        canvas.drawBitmap(bitmap, src, dst, iconPaint);

        //w往canvas 画布上添加文字信息
        Paint StrPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);

        StrPaint.setColor(Color.RED);

        StrPaint.setTextSize(20f);

        StrPaint.setTypeface(Typeface.DEFAULT_BOLD);

        canvas.drawText("正面", iconSize - 45, 20, StrPaint);

        //最终获得一个带有canvas的Bitmap:NewIcon

        return NewIcon;
    }


    //2.使用新的Bitmap在Home界面创建制定应用的启动项

    public void setHomeIcon() {
        Intent shortcutIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MaginIcon");
        Intent mainIntent = new Intent(Intent.ACTION_MAIN);

        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        mainIntent.setClass(this, MainActivityBak.class);

        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);

        sendBroadcast(shortcutIntent);

    }


    public void setIcons() {
        //创建新的启动项
        Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MagicsIcon");
        //设置不可以创建多个启动项
        shortcutIntent.putExtra("duplicate", false);
        //创建Home界面启动项
        Intent mainIntent = new Intent(Intent.ACTION_MAIN);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        mainIntent.setClass(this, MainActivityBak.class);

        PackageManager pkgMag = getPackageManager();
        Intent queryIntent = new Intent(Intent.ACTION_MAIN, null);
        queryIntent.addCategory(Intent.CATEGORY_LAUNCHER);

//        List<ResolveInfo> list = pkgMag.queryIntentActivities(queryIntent,PackageManager.GET_ACTIVITIES);
//
//        for(int i = 0;i < list.size();i++){
//            ResolveInfo info = list.get(i);
//            if(info.activityInfo.packageName.equals(pk)){
//
//            }
//        }


        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, getBitmap());

        sendBroadcast(shortcutIntent);


    }

    //------------以下为动态替换桌面应用Icon的一种解决方案-------------------


    //------------以下为生成桌面widget的几种方法,本质一样----------------

    private void shortcutAdd(String name, int number) {
        // Intent to be send, when shortcut is pressed by user ("launched")
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivityBak.class);
        //shortcutIntent.setAction(SyncStateContract.Constants.ACCOUNT_NAME);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Create bitmap with number in it -> very default. You probably want to give it a more stylish look
        Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        paint.setColor(0xFFFF0000); // gray
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(50);
        new Canvas(bitmap).drawText("" + number, 50, 50, paint);
        ((ImageView) findViewById(R.id.icon)).setImageBitmap(bitmap);

        // Decorate the shortcut
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);

        // Inform launcher to create shortcut
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }

    private void shortcutDel(String name) {
        // Intent to be send, when shortcut is pressed by user ("launched")
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivityBak.class);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Decorate the shortcut
        Intent delIntent = new Intent();
        delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);

        // Inform launcher to remove shortcut
        delIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(delIntent);
    }


    public void addShortCut() {
        Intent myLauncherIntent = new Intent(this, MainActivityBak.class);
        myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MagicIcon");
        intent.putExtra
                (
                        Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                        Intent.ShortcutIconResource.fromContext
                                (
                                        getApplicationContext(),
                                        R.mipmap.ic_launcher
                                )
                );
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(intent);
    }

 

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/324426

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。