您的位置:首页 > 移动开发 > Android开发

Android 添加和删除桌面快捷方式

2015-09-22 18:33 726 查看
原文 url http://blog.csdn.net/jjmm2009/article/details/37902949
为应用创建快捷方式目前有两种方法:

1. 程序启动时主动添加快捷方式到桌面------------>主动添加

2.长按桌面,弹出应用选择窗,拖动应用到桌面---------->被动添加

公用方法:

[java] view
plaincopy

/**

* 返回添加到桌面快捷方式的Intent:

* 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT"

* 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)

* 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT

*/

public static Intent getShortcutToDesktopIntent(Context context) {

Intent intent = new Intent();

intent.setClass(context, context.getClass());

/*以下两句是为了在卸载应用的时候同时删除桌面快捷方式*/

intent.setAction("android.intent.action.MAIN");

intent.addCategory("android.intent.category.LAUNCHER");

Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

// 不允许重建

shortcut.putExtra("duplicate", false);

// 设置名字

shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));

// 设置图标

shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));

// 设置意图和快捷方式关联程序

shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);

return shortcut;

}

一、主动添加方式:

1. 在AndroidManifest.xml中添加权限:

[java] view
plaincopy

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

2. 在启动Activity中发送广播:

[java] view
plaincopy

sendBroadcast(getShortcutToDesktopIntent(MainActivity.this));

二、被动添加方式:

1.在AndroidManifest.xml中添加权限:

[java] view
plaincopy

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

2.在AndroidManifest.xml中为主Activity添加action监听:

[java] view
plaincopy

<!-- 如果是通过桌面长按添加快捷方式,才需要添加此配置 -->

<intent-filter>

<action android:name="android.intent.action.CREATE_SHORTCUT" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

3.在启动Activity中添加广播监听:

[java] view
plaincopy

final Intent launchIntent = getIntent();

final String action = launchIntent.getAction();

if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {

Log.i(TAG, "create shortcut method one---------------- ");

setResult(RESULT_OK, ShortcutUtils.getShortcutToDesktopIntent(MainActivity.this));

finish();

}

三、删除快捷方式:

1.在AndroidManifest.xml中添加权限:

[java] view
plaincopy

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

2.代码:

[java] view
plaincopy

/**

* 删除快捷方式

* */

public static void deleteShortCut(Context context)

{

Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

//快捷方式的名称

shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));

/**删除和创建需要对应才能找到快捷方式并成功删除**/

Intent intent = new Intent();

intent.setClass(context, context.getClass());

intent.setAction("android.intent.action.MAIN");

intent.addCategory("android.intent.category.LAUNCHER");

shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);

context.sendBroadcast(shortcut);

}

四、判断快捷方式是否已创建(该方法不起作用,方法中有说明和解决方案):

1.添加权限:

[java] view
plaincopy

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

2.代码:

[java] view
plaincopy

/**

* 判断是否已添加快捷方式:

* 暂时没有方法能够准确的判断到快捷方式,原因是,

1、不同厂商的机型他的快捷方式uri不同,我遇到过HTC的他的URI是content://com.htc.launcher.settings/favorites?notify=true

2、桌面不只是android自带的,可能是第三方的桌面,他们的快捷方式uri都不同

提供一个解决办法,创建快捷方式的时候保存到preference,或者建个文件在SD卡上,下次加载的时候判断不存在就先发删除广播,再重新创建

* 添加权限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" ></uses-permission>

*/

public static boolean hasInstallShortcut(Context context) {

boolean hasInstall = false;

String AUTHORITY = "com.android.launcher.settings";

int systemversion = Build.VERSION.SDK_INT;

Log.i("Build.VERSION.SDK==========>", systemversion + "");

/*大于8的时候在com.android.launcher2.settings 里查询(未测试)*/

if(systemversion >= 8){

AUTHORITY = "com.android.launcher2.settings";

}

Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");

Cursor cursor = context.getContentResolver().query(CONTENT_URI,

new String[] { "title" }, "title=?",

new String[] { context.getString(R.string.app_name) }, null);

if (cursor != null && cursor.getCount() > 0) {

hasInstall = true;

}

return hasInstall;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: