android开发:为桌面创建一个快捷方式的代码

直接上代码:

Parcelable icon = Intent.ShortcutIconResource.fromContext(mContext,R.mipmap.ic_launcher);//桌面的图标
String appname = getString(R.string.app_name);//快捷方式的名称
Intent intent;

//添加
intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,appname);//名称
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);   //桌面图标
intent.putExtra("duplicate",false);//不允许创建多个快捷方式

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(MainActivity.this,MainActivity.class));//设置要启动的class
sendBroadcast(intent);//发送广播

Leave a Comment

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |