private static void createAppShortCut(Context context) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.setClass(context.getApplicationContext(), MainActivity.class);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Parcelable icon = Intent.ShortcutIconResource.fromContext(context,R.drawable.icon);
Intent addShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));
addShortcut.putExtra("duplicate", false);
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
context.sendBroadcast(addShortcut);
}
public static Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case Constant.HANDLER_CREATE_APP_SHORTCUT:
createAppShortCut(MainActivity.instance);
break;
}
}
}
/**
* 添加桌面快捷方式
*/
public static void addShortCut() {
if (MyUtility.AppGetFirstRunFlag()) {
if (Intent.ACTION_MAIN.equals(MainActivity.instance.getIntent().getAction())) {
MyUtility.AppSetFirstRunFlag();
handler.sendEmptyMessage(Constant.HANDLER_CREATE_APP_SHORTCUT);
}
}
}
MyUtility.AppSetFirstRunFlag();
public static void AppSetFirstRunFlag() {
int versionNum = getPackageVersionNum();
SharedPreferences settings = getSharedPreferences(PREFS_NAME);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(Constant.APP_IS_FIRST + versionNum, false);
editor.commit();
}