注意:需要添加权限<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

以下是全部代码:

package shortcut.demo;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.os.Parcelable;import android.preference.PreferenceManager;import android.widget.Toast;public class ShorcutDemoActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        makeShortCut();    }        /** * 创建桌面快捷方式 */private void makeShortCut() {SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);Editor editor = sp.edit();boolean isFirstStart = sp.getBoolean("isFirstStart", true);if(!isFirstStart) return;        Parcelable appIcon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); //获取快捷键的图标                    String appName = getString(R.string.app_name);        Intent addIntent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");         Intent myIntent=new Intent(this, ShorcutDemoActivity.class);                addIntent.putExtra("duplicate", false); //不允许重复创建        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);//快捷方式的标题                   addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appIcon);//快捷方式的图标                      addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);//快捷方式的动作            sendBroadcast(addIntent);//发送广播        editor.putBoolean("isFirstStart", false);        editor.commit();         Toast.makeText(this, "成功创建”"+ appName +"“快捷方式", Toast.LENGTH_LONG).show();}}

更多相关文章

  1. android 创建快捷方式图标
  2. Android图标靠界面的右边的实现
  3. 修改android桌面图标默认大小
  4. Android Studio快捷键、配置 Android Studio、cmake配置Android
  5. android studio 快捷键描述和设置
  6. android快捷键
  7. Android多分辨率适配实践【1】使用字体图标(内含两枚神器)

随机推荐

  1. PHP与Python进行数据交互
  2. php命令行下的常用命令总结
  3. 正确理解 PHP 的重载
  4. 2021最新swoole视频教程推荐(从入门到高级
  5. 匿名函数
  6. PHP中global与$GLOBALS的区别
  7. PHP中output buffering的原理及应用
  8. PHP将数组编码gbk和utf-8相互转换
  9. GD库生成图片验证码
  10. PHP编程20大效率要点