Android创建快捷方式方法的封装

Android创建快捷方式方法的封装

代码:
package com.shortcut.util;

import com.shortcut.main.R;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

public class ShortCut {

	public ShortCut() {

	}
	/**提示用户是否要创建对话框,用户选择"是"时创建对话框
	 *@param pkg 要创建快捷方式的应用所在的包名,即AndroidMainfest.xml中的package的值.
	 * @param cls  应用中最先启动的Activity的类名
	 * @param context
	 * @param sp 用来保存是否创建快捷方式的标识
	 * @param haveshortcut 保存是否创建快捷方式标识的健值 */
	public void initDialog(final String pkg, final String cls,
			final Context context, final SharedPreferences sp,
			final String haveshortcut) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage("是否创建快捷方式")
				.setCancelable(false)
				.setPositiveButton("yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// addShortcut();
								System.out.println("要创建快捷方式!");
								createShortCut(pkg, cls, context, sp,
										haveshortcut);
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						System.out.println("不要创建快捷方式!");
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		boolean bl = sp.getBoolean(haveshortcut, false);
		if (!bl) {
			alert.show();
		} else {
			System.out.println("已有快捷方式!");
		}

	}
	/**创建快捷方式的方法
	 * @param pkg 要创建快捷方式的应用所在的包名,即AndroidMainfest.xml中的package的值.
	 * @param cls  应用中最先启动的Activity的类名
	 * @param context
	 * @param sp 用来保存是否创建快捷方式的标识
	 * @param haveshortcut 保存是否创建快捷方式标识的健值
	 * 
	 * */
	public void createShortCut(String pkg, String cls, Context context,
			SharedPreferences sp, String haveshortcut) {
		Intent shortcut = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				context.getString(R.string.app_name));
		shortcut.putExtra("duplicate", false); // 不允许重复创建
		// 指定快捷方式启动的对象
		ComponentName comp = new ComponentName(pkg, cls);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
				Intent.ACTION_MAIN).setComponent(comp));
		// 快捷方式的图标
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
				context, R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
		context.sendBroadcast(shortcut);

		Editor editor = sp.edit();
		editor.putBoolean(haveshortcut, true);
		editor.commit();

	}

	/**判断应用是不是第一次启动,以及如果没有建立快捷方式,则弹出对话框,让用户选择是否创建快捷方式。
	 * @param pkg 要创建快捷方式的应用所在的包名,即AndroidMainfest.xml中的package的值.
	 * @param cls  应用中最先启动的Activity的类名
	 * @param context
	 * @param sp 用来保存是否创建快捷方式的标识
	 * @param haveshortcut 保存是否创建快捷方式标识的键值
	 * @param first_start 保存是否是第一次启动程序标识的键值
	 * @param spname  在shared_prefs中要保存的文件的名字
	 * */
	public void firstOrNot(Context context, String pkg, String spname,
			SharedPreferences sp, String first_start, String haveshortcut,
			String cls) {

		try {
			sp = context.getSharedPreferences(spname, Context.MODE_PRIVATE);
			boolean bl = sp.getBoolean(first_start, true);
			if (bl) {
				System.out.println("这是第一次启动程序!");
				SharedPreferences.Editor editor = sp.edit();
				editor.putBoolean(first_start, false);
				PackageInfo info = context.getPackageManager().getPackageInfo(
						pkg, 0);
				String currentVersion = info.versionName;
				int currentVersionCode = info.versionCode;
				editor.putString("versionName", currentVersion);
				editor.putInt("versionCode", currentVersionCode);
				editor.commit();
			} else {
				System.out.println("这不是第一次启动程序!");

			}
			boolean isHaveShortCut = sp.getBoolean(haveshortcut, false);
			if (isHaveShortCut) {
				System.out.println("已经有了快捷方式!");
			} else {
				System.out.println("还没有快捷方式!");
				initDialog(pkg, cls, context, sp, haveshortcut);

			}
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

只要没有创建快捷方式,每次运行这个程序时都会提示用户是否创建快捷方式。
完整的例子可以到 http://download.csdn.net/download/qyqingyan/5806941下载!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值