Android 开机自动运行和添加删除桌面快捷方式

<一>开机自启动
当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之即可。我们要做的是做好接收这个消息的准备,而实现的手段就是实现一个BroadcastReceiver。
1.首先定义一个BroadcastReceiver,覆写其onReceive()方法,在里面判断intent是否是开机启动广播,如果是的话就进行相应的处理;

public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(BOOT_ACTION)) {
            // doSomething(startService or startAcvitity or downLoadFile ...)
        }
    }
}

2.在Manifest文件中进行配置,intent-filter表示该Receiver接收的广播消息为:android.intent.action.BOOT_COMPLETED;

        <receiver android:name="com.xxx.BootBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- uses 权限 -->

<二>添加删除桌面快捷方式
有时候希望自动将程序快捷方式添加到桌面,最近在一个项目中,就遇到这样的需求,现将自己在做法进行总结及延伸。
1.添加:查看Launcher源码,查看是如何添加桌面快捷方式的,发现Launcher通过自己注册的InstallShortCutReceiver和UnInstallShortCutReceiver实现快捷方式图标的生成与移除过程;

        <receiver
            android:name="com.android.launcher2.InstallShortcutReceiver"<!--SDK版本小于8时为launcher-->
            android:permission="com.android.launcher.permission.INSTALL_SHORTCUT" >
            <intent-filter>
                <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
            </intent-filter>
        </receiver>

于是乎就可以发送一个广播给Launcher,Launcher接收到此广播之后就可以将快捷方式添加到桌面,并且需要添加权限

    public void addShortcut() {
        // 创建快捷方式的Intent
        Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        // 不允许重复创建
        shortcutIntent.putExtra("duplicate", false);
        // 快捷方式的名称
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
                getString(R.string.app_name));
        // 快捷图片,一个Parcelable对象
        Parcelable icon = Intent.ShortcutIconResource.fromContext(
                getApplicationContext(), R.drawable.ic_launcher);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.setAction("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");

        // 点击快捷图片,运行的程序主入口
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        sendBroadcast(shortcutIntent);
    }

添加权限:

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

2.删除:删除快捷方式用得不多,上面的方式添加到桌面的快捷方式,在程序卸载的时候也会自动从桌面删除;

    public static void delShortcut(Context context) {
        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
        // 获取当前应用名称的另一种方式
        String title = null;
        try {
            final PackageManager pm = context.getPackageManager();
            title = pm.getApplicationLabel(
                    pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)).toString();
        } catch (Exception e) {
        }
        // 快捷方式名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        context.sendBroadcast(shortcut);
    }
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

3.判断桌面快捷方式是否已经存在

    public static boolean hasShortcut(Context cx) {
        boolean result = false;
        // 获取当前应用名称
        String title = null;
        try {
            final PackageManager pm = cx.getPackageManager();
            title = pm.getApplicationLabel(
                    pm.getApplicationInfo(cx.getPackageName(), PackageManager.GET_META_DATA)).toString();
        } catch (Exception e) {
        }

        final String uriStr;
        if (android.os.Build.VERSION.SDK_INT < 8) {
            uriStr = "content://com.android.launcher.settings/favorites?notify=true";
        } else {
            uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
        }
        final Uri CONTENT_URI = Uri.parse(uriStr);
        final Cursor c = cx.getContentResolver().query(CONTENT_URI, null, "title=?", new String[] { title }, null);
        if (c != null && c.getCount() > 0) {
            result = true;
        }
        return result;
    }
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

几个相关的Action

    // 系统启动完成
    static final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
    // 设备上新安装了一个应用程序包
    static final String PACKAGE_ADDED_ACTION = "android.intent.action.PACKAGE_ADDED";
    // 设备上删除了一个应用程序包
    static final String PACKAGE_REMOVED_ACTION = "android.intent.action.PACKAGE_REMOVED";
    // 删除应用程序快捷方式,需要如下权限
    // com.android.launcher.permission.UNINSTALL_SHORTCUT
    static final String UNINSTALL_SHORTCUT_ACTION = "com.android.launcher.action.UNINSTALL_SHORTCUT";
    // 添加快捷方式,需要如下权限
    // com.android.launcher.permission.INSTALL_SHORTCUT
    static final String INSTALL_SHORTCUT_ACTION = "com.android.launcher.permission.INSTALL_SHORTCUT";

4.监听app安装/卸载过程,需要用到上面的PACKAGE_ADDED和PACKAGE_REMOVED两个Action,可以对获取到的应用程序包名进行相应的判断处理;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(PACKAGE_ADDED_ACTION)) {
            // doSomething ...获取应用程序包名
            String packageName = intent.getDataString();
        }
    }

添加如下配置,对Receiver进行配置

<receiver android:name="com.example.async.BootBroadcastReceiver" >
      <intent-filter>
           <action android:name="android.intent.action.PACKAGE_ADDED" /> <!--PACKAGE_REMOVED-->
           <data android:scheme="package" />  <!-- 一定要添加此节点 -->
      </intent-filter>
</receiver>
原文地址:https://www.cnblogs.com/a284628487/p/3136720.html