android创建快捷方式,代码如下:
/** add shortcut to the application
* @param context
*/
public void createShortCut(Context context) {
final Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
final Parcelable icon = Intent.ShortcutIconResource.fromContext(context, R.drawable.ehislogo); // icon
addIntent.putExtra("duplicate", false);
final Intent myIntent = new Intent(context, Login.class);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));// title
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// setting icon
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// shortcut action
context.sendBroadcast(addIntent);
}
所需权限如下:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
注意事项:
创建快捷方式的时候一般就第一次打开应用的时候才创建,所以还需进行是否是第一次使用的判断,代码如下:
Boolean isFirstIn = false;
SharedPreferences pref = mContext.getSharedPreferences("myActivityName", 0);
//取得相应的值,如果没有该值,说明还未写入,用true作为默认值
isFirstIn = pref.getBoolean("isFirstIn", true);
if(isFirstIn){
createShortCut();//创建快捷方式
SharedPreferences pref2 = mContext.getSharedPreferences("myActivityName", 0);
Editor editor = pref2.edit();
editor.putBoolean("isFirstIn", false);
editor.commit();
}