/** 创建快捷方式 * */ public void createDeskShortCut() { //创建快捷方式的Intent Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //不允许重复创建 ,如果重复的话就会有多个快捷方式了 shortcutIntent.putExtra("duplicate",false); //这个就是应用程序图标下面的名称 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name)); //快捷图片 Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.login_icon); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon); Intent intent = new Intent(getApplicationContext(),ClassifyActivity.class); //这个MainActivity是调用此方法的Activity //下面两个属性是为了当应用程序卸载时桌面 上的快捷方式会删除 intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); intent.setClass(this, WelcomeActivity.class); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent); //最后一步就是发送广播 sendBroadcast(shortcutIntent); }
下面是如何调用:
判断SharedPreferences 是否存在,不存在就证明程序第一次安装启动,创建串接方式
SharedPreferences preferences = getSharedPreferences("isfrist_file",ClassifyActivity.MODE_PRIVATE); boolean isFirst = preferences.getBoolean("isfrist", true); //就是第一次运行程序时就创建桌面快捷方式,以后就不创建了 if(isFirst) { // DefaultDialog dd = new DefaultDialog(this, true) { // // @Override // protected void doPositive() { // createDeskShortCut(); // } // }; // dd.setMessage(R.string.shortcut); // dd.show(); createDeskShortCut(); } SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("isfrist",false); editor.commit();