Android(安卓)-- App生成快捷方式
16lz
2022-06-12
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //只第一次运行时生成,以后就不生成了 SharedPreferences preferences = getSharedPreferences("isfrist_file", Context.MODE_PRIVATE); boolean isFirst = preferences.getBoolean("isfrist", true); if (isFirst) { //创建快捷方式 createDeskShortCut(); } SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("isfrist", false); editor.commit(); } public void createDeskShortCut() { // 创建快捷方式的Intent Intent shortcut = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); // 不允许重复创建 shortcut.putExtra("duplicate", false); // 需要现实的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); // 快捷图片 Parcelable icon = Intent.ShortcutIconResource.fromContext( getApplicationContext(), R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 快捷方式入口 Intent intent = new Intent(getApplicationContext(), MainActivity.class); // 下面两个属性是为了当应用程序卸载时,删除桌面上的快捷方式 intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); // 点击快捷图片,运行的程序主入口 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // 发送广播 OK sendBroadcast(shortcut); }}
还要添加创建快捷方式权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
更多相关文章
- Android之ShortCut[setResult方式]
- AIDL(android 接口定义语言)
- Android(安卓)总结 Android(安卓)系统架构
- Android(安卓)Too many classes in --main-dex-list 错误原因及A
- 天天记录 - Android创建应用窗口分析2
- 关于android应用程序的入口
- android 单元测试cmd 命令集
- android布局动画实现方法
- Android(安卓)== 在Android系统上运行JAVA程序