Android桌面快捷方式
来源:互联网 发布:知乎自考可以跨专业吗 编辑:程序博客网 时间:2024/06/01 09:00
这篇博客,我们主要讨论一下Android中的桌面快捷方式的开发。。。
废话不多说了。。。
直接实战
1.判断快捷方式是否创建。
/** * 验证是否创建快捷方式* * @return */public boolean isAddShortcut() {final ContentResolver mContentResolver=mContext.getContentResolver();int versionLevel=Build.VERSION.SDK_INT;String AUTHORITY = "com.android.launcher2.settings";if(versionLevel>=8){AUTHORITY="com.android.launcher2.settings";}else{AUTHORITY="com.android.launcher.settings";}final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = mContentResolver.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { mContext.getString(R.string.app_name) }, null); if (c != null && c.getCount() > 0) { return true; }return false;}
2.添加快捷方式。
/*** 创建快捷方式*/public void addShortcut() {Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 设置属性 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(R.string.app_name)); ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext.getApplicationContext(), R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes); // 是否允许重复创建 shortcut.putExtra("duplicate", false); //设置桌面快捷方式的图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(mContext,R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon); //点击快捷方式的操作 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(mContext, MainActivity.class); // 设置启动程序 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); //广播通知桌面去创建 mContext.sendBroadcast(shortcut);}
3.删除快捷方式。
/*** 删除快捷图标*/public void deleteShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); //快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,mContext.getString(R.string.app_name)); /**删除和创建需要对应才能找到快捷方式并成功删除**/ Intent intent = new Intent(); intent.setClass(mContext, MainActivity.class); intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent); mContext.sendBroadcast(shortcut); }
4.权限的加入。
<!-- 创建快捷方式权限 --> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/><uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
5.快捷方式启动的Activity加入intent-filter,如下:
<intent-filter > <action android:name="android.intent.action.CREATE_SHORTCUT"></action></intent-filter>
上一份源码:
/** * */package com.hanfeng.shortcut;import android.content.ContentResolver;import android.content.Context;import android.content.Intent;import android.content.Intent.ShortcutIconResource;import android.database.Cursor;import android.net.Uri;import android.os.Build;import android.os.Parcelable;/** * * 快捷方式处理 * * @author hanfeng * @time 2014-12-3 下午1:48:01 * */public class ShorcutHandler {private Context mContext;/*** @param mContext*/public ShorcutHandler(Context mContext) {super();this.mContext = mContext;}/*** 创建快捷方式*/public void addShortcut() {Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 设置属性 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(R.string.app_name)); ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext.getApplicationContext(), R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes); // 是否允许重复创建 shortcut.putExtra("duplicate", false); //设置桌面快捷方式的图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(mContext,R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon); //点击快捷方式的操作 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(mContext, MainActivity.class); // 设置启动程序 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); //广播通知桌面去创建 mContext.sendBroadcast(shortcut);}/*** 删除快捷图标*/public void deleteShortcut(){Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); //快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,mContext.getString(R.string.app_name)); /**删除和创建需要对应才能找到快捷方式并成功删除**/ Intent intent = new Intent(); intent.setClass(mContext, MainActivity.class); intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent); mContext.sendBroadcast(shortcut); }/*** 验证是否创建快捷方式* * @return*/public boolean isAddShortcut() {final ContentResolver mContentResolver=mContext.getContentResolver();int versionLevel=Build.VERSION.SDK_INT;String AUTHORITY = "com.android.launcher2.settings";if(versionLevel>=8){AUTHORITY="com.android.launcher2.settings";}else{AUTHORITY="com.android.launcher.settings";}final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = mContentResolver.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { mContext.getString(R.string.app_name) }, null); if (c != null && c.getCount() > 0) { return true; }return false;}}
到此,Android的快捷方式的开发完毕。。。
源码下载
0 0
- android桌面添加快捷方式
- Android桌面快捷方式
- Android桌面快捷方式
- Android 桌面快捷方式操作
- android 桌面快捷方式
- Android桌面快捷方式
- android 创建桌面快捷方式
- Android添加桌面快捷方式
- Android 创建桌面快捷方式
- android创建桌面快捷方式
- Android 创建桌面快捷方式
- android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android创建桌面快捷方式
- android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android 创建桌面快捷方式
- UVa 10905 - Children's Game
- 年轻如初升的太阳
- 基于PaaS平台租户部署及人员跨部门设计方案
- 学习标签更新20141206
- 03.Git 的origin和master分析
- Android桌面快捷方式
- ffmpeg 转码 原始视频
- Hadoop MapReduce2 -单节点集群配置
- poj 2029 Get Many Persimmon Trees 暴力枚举
- Android下屏保程序的开发
- JAVA字符串格式化-String.format()的使用
- 一个程序员的个人思考
- win8.1下codeblocks + MinGW 配置OpenCV2.4.10
- JSP中WebContent和WEB-INF下网页的路径问题和404报错