<一>开机自启动
当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";@Overridepublic 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实现快捷方式图标的生成与移除过程;

        <receiverandroid: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() {// 创建快捷方式的IntentIntent 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_SHORTCUTstatic final String UNINSTALL_SHORTCUT_ACTION = "com.android.launcher.action.UNINSTALL_SHORTCUT";// 添加快捷方式,需要如下权限// com.android.launcher.permission.INSTALL_SHORTCUTstatic final String INSTALL_SHORTCUT_ACTION = "com.android.launcher.permission.INSTALL_SHORTCUT";

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

    @Overridepublic 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>

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

  1. Android开机自动运行APP——BroadcastReceiver

    前言: 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以"Don't call me, I' ...

  2. android开机自动运行程序

    背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为 android.intent.action.BOOT_COMPLETED. ...

  3. wince6.0桌面背景+设置默认IP+开机自动运行程序

    前几天一直很苦恼,找不到方法,恨不得把自己扔到大海里去. 没想到,星星之火真可以燎原,因为一点小地方想明白了,接下来的问题全都迎刃而解. 做个小小的总结吧. 1.桌面背景的切换,要修改shell.bi ...

  4. android 开机直接运行app并当做手机桌面

    android 开机直接运行app并当做手机桌面 直接上代码: 1.开机启动APP 1.1 写一个广播接收器,用来接收手机开机广播 public class Receiver extends Broa ...

  5. Win10添加开机自动运行软件三种方法(亲测可用)

    Win10管理开机启动项的方法相信大家已经非常熟悉,msconfig命令各系统都通用,那么很多用户发觉Win10和Win7 XP等系统不同,没有启动文件夹,那么我们怎么添加开机启动项呢?如晨软件或程序 ...

  6. Android实现开机自动运行程序

    有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以"Don't call me, I'll c ...

  7. 详细说明Win10添加开机自动运行软件3种方法

    文章目录 1. 文章引言 2. 开机自动运行的3种方法 2.1 方法1:开机启动文件夹 2.2 方法2:注册表添加启动项 2.3 方法3:任务计划程序 3. 文末总结 1. 文章引言 每次启动电脑,都 ...

  8. Android 创建,验证和删除桌面快捷方式 (删除快捷方式测试可用)

    测试环境为Adnroid 2.1以上. 第一步:AndroidManifest.xml 权限配置: 添加快捷方式权限: <uses-permission android:name="c ...

  9. outlook邮箱显示一直启动中_win10outlook邮箱怎么设置开机自动运行 - 卡饭网

    windows xp系统下如何设置开机自动运行程 windows xp系统下如何设置开机自动运行程 设置开机自动运行的步骤方法如下: 点击桌面左下角开始菜单,运行设置中的控制面板. 在打开的控制面板中 ...

最新文章

  1. 艾伟_转载:学习 ASP.NET MVC (第五回)理论篇
  2. 人民日报智慧媒体研究院与第四范式合资成立智媒新创 赋能智慧媒体创新
  3. 干货 | extern的用法解析
  4. python decorator. decorator_Python中decorator使用实例
  5. php定义object数据类型,PHP数据类型(4):对象object
  6. php模拟远程提交get 、post 实例函数
  7. Linux多线程详解
  8. 数据库练习题总题库选择判断简答操作题
  9. 电子信息工程这个专业学的是什么内容,就业怎么样?
  10. php常用编码,简介常见的编码方式
  11. Java实现下载url视频资源
  12. 不定式和动名词复合结构是什么
  13. spdep | 如何在R语言中计算空间自相关指数
  14. UltraEdit\UEStudio 的 SSHTelnet 功能教程
  15. Mybati从持久层到大气层
  16. 管不住嘴、挪不动腿?
  17. 刀客建站系统安装流程
  18. Virgo与Maven整合开发环境搭建(三)
  19. Pedersen commitment
  20. 2021ccpc桂林小结

热门文章

  1. 【GAN优化】从动力学视角看GAN是一种什么感觉?
  2. 【知识星球】softmax损失相关的小问题
  3. 【行业进展】AI:新药研发的新纪元
  4. 廊田镇楼下村定点帮扶-农业大健康·李喜贵:功能性农业深加工
  5. python自学第12天 模块
  6. Bootstrap 表格
  7. [Luogu] 1600
  8. leetcode 27. Remove Element
  9. HNOI2017 day1 T3 礼物
  10. HTML 元素居中的方法