java – 添加Android应用程序的快捷方式到主屏幕按钮点击

我想通过按一下按钮轻松地将我的应用程序添加到主屏幕.所以我在想什么是我的应用程序底部一个按钮,它表示“添加到主屏幕”,按下它后,它将快捷方式添加到主屏幕,而不关闭应用程序.
我应该添加什么代码呢?

解决方法

发送INSTALL_SHORTCUT广播与产生的Intent作为额外的(在这种情况下,结果Intent直接打开一些活动).
//where this is a context (e.g. your current activity)
    final Intent shortcutIntent = new Intent(this,SomeActivity.class);

    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,R.drawable.icon));
    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendbroadcast(intent);

您的清单还需要此权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...