快捷方式:给APP内部某一个Activity创建快捷方式(类似QQ好友快捷方式)
2014-12-17 09:25
417 查看
第一步:添加权限:
第二步:
为应用程序组件注册一个符合特定条件的IntentFilter,然后就可以直接在Launcher的桌面上添加启动该组件的快捷方式了。
当我们想把添加的快捷方式的Activity添加进这列时,只需要在这个Activity注册时添加一个Action为android.intent.action.CREATE_SHORTCUT的IntentFilter就可以。
若不注册一个符合特定条件的IntentFilter,创建快捷方式成功后,点击快捷方式时,则会提示,您的手机未安装应用程序。
第三步:创建快捷方式:
另,附快捷方式其他操作:
<!-- 桌面快捷方式 --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
第二步:
为应用程序组件注册一个符合特定条件的IntentFilter,然后就可以直接在Launcher的桌面上添加启动该组件的快捷方式了。
当我们想把添加的快捷方式的Activity添加进这列时,只需要在这个Activity注册时添加一个Action为android.intent.action.CREATE_SHORTCUT的IntentFilter就可以。
若不注册一个符合特定条件的IntentFilter,创建快捷方式成功后,点击快捷方式时,则会提示,您的手机未安装应用程序。
<activity android:name="com.qianyanshangwu.qianyanlife.BusinessListActivity" android:screenOrientation="portrait" android:theme="@style/QianyanTheme.Translucent.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> </intent-filter> </activity>
第三步:创建快捷方式:
/** * 创建指定Activity的快捷方式 * * @param context * @param cls * 目标Activity * @param bundle * 传递给Activity的数据 * @param name * 快捷方式标题 * @param resId * 快捷方式图标 */ public static void createShortCut(Context context, Class<?> cls, Bundle bundle, String name, int resId) { Intent targetIntent = new Intent(); targetIntent.setClass(context, cls); if (bundle != null) { targetIntent.putExtras(bundle); } Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); Parcelable icon = Intent.ShortcutIconResource.fromContext(context, resId); // 获取快捷键的图标 shortcutIntent.putExtra("duplicate", false); // 设置快捷方式不能重复 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);// 快捷方式的标题 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);// 快捷方式的动作 context.sendBroadcast(shortcutIntent); }
另,附快捷方式其他操作:
import java.util.HashMap; import java.util.Map; import android.app.AlertDialog; import android.app.Dialog; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.util.DisplayMetrics; import com.qianyanshangwu.qianyanlife.BusinessListActivity; import com.qianyanshangwu.qianyanlife.R; import com.qianyanshangwu.qianyanlife.SplashActivity; /** * APP信息 * * @author zkw * */ public class AppUtils { /** * * 屏幕高 * * @param context * @return */ public static int getScreenWidth(Context context) { DisplayMetrics dm = new DisplayMetrics(); dm = context.getResources().getDisplayMetrics(); return dm.widthPixels; } /** * 获取当前应用程序的版本号 * * @return */ public static String getVersion(Context context) { try { // 获取清单文件中当前程序的版本号 // 要想获取到清单文件,首先获取当前apk对应的包 PackageManager manager = context.getPackageManager(); // 第一个参数:包名,既可以直接写"cn.example.phonesafe",也可以通过getPackageName获取 // 第二个参数不关心,设置为0 PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);// 获取包的信息 return info.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); return "0"; } } /** * 是否已创建快捷方式 * * @param context * @return */ public static boolean hasShortcut(Context context) { boolean isInstallShortcut = false; final String AUTHORITY = "com.android.launcher.settings"; final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { context.getString(R.string.app_name).trim() }, null); if (null != c && c.getCount() > 0) { isInstallShortcut = true; } return isInstallShortcut; } /** * 创建桌面快捷方式 */ public static void createShortCutDialog(final Context context) { Dialog alertDialog = new AlertDialog.Builder(context)// .setTitle("温馨提示").setMessage("是否创建桌面快捷方式?").setIcon(R.drawable.ic_launcher)// .setPositiveButton("创建", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { createShortCut(context); } })// .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create(); alertDialog.show(); } /** * 创建快捷方式 */ public static void createShortCut(Context context) { Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); Parcelable icon = Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher); // 获取快捷键的图标 shortcutIntent.putExtra("duplicate", false); // 设置快捷方式不能重复 Intent targetIntent = new Intent(context, SplashActivity.class); // 卸载程序后删除快捷方式 targetIntent.setAction("android.intent.action.MAIN"); targetIntent.addCategory("android.intent.category.LAUNCHER"); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));// 快捷方式的标题 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);// 快捷方式的动作 context.sendBroadcast(shortcutIntent); PreferencesManager.putBoolean(context, "SHORTCUT", true); } /** * 创建指定Activity的快捷方式 * * @param context * @param cls * 目标Activity * @param bundle * 传递给Activity的数据 * @param name * 快捷方式标题 * @param resId * 快捷方式图标 */ public static void createShortCut(Context context, Class<?> cls, Bundle bundle, String name, int resId) { Intent targetIntent = new Intent(); targetIntent.setClass(context, cls); if (bundle != null) { targetIntent.putExtras(bundle); } Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); Parcelable icon = Intent.ShortcutIconResource.fromContext(context, resId); // 获取快捷键的图标 shortcutIntent.putExtra("duplicate", false); // 设置快捷方式不能重复 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);// 快捷方式的标题 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);// 快捷方式的动作 context.sendBroadcast(shortcutIntent); } }
相关文章推荐
- 用js文件创建一个带参数的view client的快捷方式(类似的bat形式也可模仿实现),并实现自动登录
- ShortCut的延伸功能--为一个app的不同界面创建不同的快捷方式
- 【起航计划 022】2015 起航计划 Android APIDemo的魔鬼步伐 21 App->Launcher Shortcuts 为某个非主Activity在Home Screen上建立一个快捷方式
- Android_如何为一个app创建桌面快捷方式
- 【起航计划 022】2015 起航计划 Android APIDemo的魔鬼步伐 21 App-&gt;Launcher Shortcuts 为某个非主Activity在Home Screen上建立一个快捷方式
- Android 为应用程序中的任意Activity创建桌面快捷方式
- Python 最近因开发项目的需要,有一个需求,就是很多SNS网站都有的通过 Email地址 导入好友列表,不过这次要导入的不是Email 列表,而是QQ的好友列表。 实现方式: 通过goog
- C#创建一个文件的快捷方式
- App快捷方式的创建
- C# 创建一个文件夹快捷方式
- C#创建一个文件的快捷方式
- Android创建桌面快捷方式并像启动Activity传递参数
- 在iphone/touch上为网页创建快捷方式APP
- Win8中创建一个快速关机按钮快捷方式图文教程
- android应用内为某个Activity创建快捷方式后按home键的问题
- Ubuntu下如何在桌面创建一个桌面快捷方式
- 三星 Galaxy Nexus,4.0.3/华为荣耀U8860 从程序自动创建快捷方式进入报 [没有找到Activity]
- 创建一个用于建立快捷方式的COM 对象并取得IShellLink 接口。
- 开机默认设置一个widge在指定的屏幕上和自动在桌面上创建一个应用的快捷方式的方法
- Android中为APP创建快捷方式的原理(自己的理解)