public void checkShortCut() {
final String vString = getAppVersionName(MyAppTest.this);
final SharedPreferences sp = getSharedPreferences("csw", 0);
// 是否在桌面上添加了快捷方式
boolean never_check_shortCut = sp.getBoolean("isShowIcon" + vString,
false);
// 存在快捷方式或者不允许添加,return
if (never_check_shortCut) {
return;
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(MyAppTest.this);
builder.setTitle("温馨提示");
builder.setMessage("是否创建桌面快捷方式?");
builder.setPositiveButton("创建", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
addShortCut();
// 保存已经添加了快捷方式的信息,以便程序下次启动的不再提示
Editor editor = sp.edit();
editor.putBoolean("isShowIcon" + vString, true);
editor.commit();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});
builder.create().show();
}
}

public void addShortCut() {
// 添加快捷方式
// 指定快捷方式的Action
Intent installShortCut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 添加快捷方式的名称
installShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
installShortCut.putExtra("duplicate", false);

// 下面NewAppActivity是我这边当前的activity名字,用的时候可以适当改成你自己的activity名
ComponentName comp = new ComponentName(MyAppTest.this.getPackageName(),
"." + MyAppTest.this.getLocalClassName());
installShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
Intent.ACTION_MAIN).setComponent(comp));
// 指定图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
MyAppTest.this, R.drawable.icon);
installShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
// 发送广播
sendBroadcast(installShortCut);
}

public String getAppVersionName(Context context) {
String versionName = "";
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
versionName = pi.versionName;
if (versionName == null || versionName.length() <= 0) {
return "";
}
} catch (Exception e) {
Log.e("VersionInfo", "Exception", e);
}
return versionName;
}

posted on 2012-02-22 09:07  轻盈  阅读(856)  评论(0编辑  收藏  举报