Android应用程序创建桌面快捷方式

测试环境为Adnroid 2.1以上。
第一步:AndroidManifest.xml 权限配置: 
添加快捷方式权限:
 
验证快捷方式是否存在权限: 

删除快捷方式权限: 

代码如下:
创建ShortCutSample类
package cn.haokuai.maya.suppliers.app;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcelable;

public class ShortCutSample {
	/**
	 * 添加快捷方式
	 * */
	public void creatShortCut(Activity activity, String shortcutName,
			int resourceId) {
		Intent intent = new Intent();
		intent.setClass(activity, AppStart.class);
		/* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");
		Intent shortcutintent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		// 不允许重复创建
		shortcutintent.putExtra("duplicate", false);
		// 需要现实的名称
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
		// 快捷图片
		Parcelable icon = Intent.ShortcutIconResource.fromContext(
				activity.getApplicationContext(), resourceId);
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		// 点击快捷图片,运行的程序主入口
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		// 发送广播。OK
		activity.sendBroadcast(shortcutintent);
	}

	/**
	 * 删除快捷方式
	 * */
	public void deleteShortCut(Activity activity, String shortcutName) {
		Intent shortcut = new Intent(
				"com.android.launcher.action.UNINSTALL_SHORTCUT");
		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
		// 在网上看到到的基本都是一下几句,测试的时候发现并不能删除快捷方式。
		// String appClass = activity.getPackageName()+"."+
		// activity.getLocalClassName();
		// ComponentName comp = new ComponentName( activity.getPackageName(),
		// appClass);
		// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new
		// Intent(Intent.ACTION_MAIN).setComponent(comp));
		/** 改成以下方式能够成功删除,估计是删除和创建需要对应才能找到快捷方式并成功删除 **/
		Intent intent = new Intent();
		intent.setClass(activity, activity.getClass());
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		activity.sendBroadcast(shortcut);
	}

	/**
	 * 判断是否存在快捷方式
	 * */
	public boolean hasShortcut(Activity activity, String shortcutName) {
		String url = "";
		int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK);
		/* 大于8的时候在com.android.launcher2.settings 里查询(未测试) */
		if (systemversion < 8) {
			url = "content://com.android.launcher.settings/favorites?notify=true";
		} else {
			url = "content://com.android.launcher2.settings/favorites?notify=true";
		}
		ContentResolver resolver = activity.getContentResolver();
		Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",
				new String[] { shortcutName }, null);
		if (cursor != null && cursor.moveToFirst()) {
			cursor.close();
			return true;
		}
		return false;
	}
}
调用测试代码
public class Welcome extends Activity {
    
	private static final String TAG  = "Welcome";
	 final AppContext ac = (AppContext) getApplication();
	 private Button btn;
	 private ViewFlow viewFlow;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final View view = View.inflate(this, R.layout.welcome, null);
        setContentView(view);
        ShortCutSample sample =new ShortCutSample(); 
		String shortcutName=getString(R.string.app_name); 
		if(sample.hasShortcut(this, shortcutName)) 
			sample.deleteShortCut(this,shortcutName); 
		else 
			sample.creatShortCut(this,shortcutName,R.drawable.ic_launcher); 
}}



你可能感兴趣的:(Android,Android,android应用,launcher,桌面快捷方式)