Android中的快捷方式(本文只包含检查快捷方式的创建)

来源:互联网 发布:软件采购招标评分标准 编辑:程序博客网 时间:2024/06/03 22:52

前言

这次准备写一系列文章来总结自己项目中的一些知识点和自己的一些经验。如标题本文只包含快捷方式的创建(删除操作用户直接在桌面就删除了,检查是否存在需要适配比较麻烦就没有检查,在项目中耍个小聪明直接就给跳过去了)。快捷方式的创建分三部分,1.权限申请,2.代码中发广播创建快捷方式,3.清单中注册。本文比较简单,只涉及如何使用没有原理的东西(第一篇先简单点吧)。

这里写图片描述

权限申请

6.0以上要动态申请权限比较简单就直接上代码了, getShortcut()方法就是发广播创建快捷方式:

 private void createShortcut() {        if (Build.VERSION.SDK_INT >= 23) {            if (ContextCompat.checkSelfPermission(this,                    Manifest.permission.UNINSTALL_SHORTCUT)                    != PackageManager.PERMISSION_GRANTED) {                Log.e("==", "1");                if (ActivityCompat.shouldShowRequestPermissionRationale(this,                        Manifest.permission.UNINSTALL_SHORTCUT)) {                    Log.e("==", "2");                } else {                    ActivityCompat.requestPermissions(this,                            new String[]{Manifest.permission.UNINSTALL_SHORTCUT},                            REQUEST_CODE_REQUEST_PERMISSION);                    Log.e("==", "3");                }            } else {                getShortcut();            }        } else {            getShortcut();        }    }

复写权限申请结果

 @Override    public void onRequestPermissionsResult(int requestCode,                                           String permissions[], int[] grantResults) {        switch (requestCode) {            case REQUEST_CODE_REQUEST_PERMISSION: {                if (grantResults.length > 0                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                    getShortcut();                } else {                    Log.e("lv", "4");                }                return;            }        }    }

创建快捷方式

 private void getShortcut() {        //shortcut start        Intent shortcutIntent = new Intent();        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        shortcutIntent.setAction(Intent.ACTION_MAIN);        shortcutIntent.addCategory("随意的字符串");//跟ShortCutActivity列表清单中的一致就行了        Intent resultIntent = new Intent();        resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,                Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_boost));//快解放和死的图标        resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);        resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.shortcut_label));//快捷方式的名字        resultIntent.putExtra("duplicate", false);//是否重复创建,一般都是false        resultIntent.setAction(ACTION_INSTALL_SHORTCUT);        sendBroadcast(resultIntent);//发广播交给系统创建        //shortcut end    }

大家可以看到快捷方式的创建很简单的就是发一个广播就OK了。

清单描述

 <activity            android:name=".mvp.view.ui.ShortCutActivity"            android:excludeFromRecents="true"//为了不让快捷方式在最近任务栏(多任务列表)中出现            android:taskAffinity="com.hah.task"//这个自己随意写            android:screenOrientation="portrait"            android:exported="true"            android:theme="@style/Transparent">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.DEFAULT"/>                <category android:name="随意"/>            </intent-filter>        </activity>

这个就是注册点击快捷方式以后要显示的Activity

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />//这个还是需要的
0 0