java 为android应用程序添加快捷方式到主屏幕点击按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10343414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Shortcut for android application To home screen On button click
提问by CreeperHunter
I want to make it easy to add my app to home screen by pressing a button. So What I am Thinking is a button at the bottom of my app that says "Add to home screen" and when it is pressed, it adds the shortcut to the home screen without closing the application. what code should I add To do that?
我想通过按一个按钮来轻松地将我的应用程序添加到主屏幕。所以我在想的是我的应用程序底部的一个按钮,上面写着“添加到主屏幕”,当它被按下时,它会在不关闭应用程序的情况下将快捷方式添加到主屏幕。我应该添加什么代码来做到这一点?
回答by roflharrison
Send an INSTALL_SHORTCUT broadcast with the resulting Intent as an extra (in this case, the result Intent is opening some activity directly).
发送带有结果 Intent 的 INSTALL_SHORTCUT 广播作为额外(在这种情况下,结果 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);
You also need this permission in your manifest:
您还需要在清单中获得此权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
回答by Laolizi
First step,you should make the luncher could receive a broadcast:
第一步,你应该让午餐者可以接收广播:
<!-- Intent received used to install shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.InstallShortcutReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
</intent-filter>
</receiver>
Next,add a permission in manifest.xml
接下来,在 manifest.xml 中添加一个权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
Finally,create a function and call it when you click the button:
最后,创建一个函数并在单击按钮时调用它:
public void createShortCut(){
// a Intent to create a shortCut
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//repeat to create is forbidden
shortcutintent.putExtra("duplicate", false);
//set the name of shortCut
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
//set icon
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//set the application to lunch when you click the icon
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
//sendBroadcast,done
sendBroadcast(shortcutintent);
}
do it like this:
像这样做:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
createShortCut();
}
});
回答by siva
Ok ... I know this is old thread but i wanted to make sure engineers visiting this thread have latest information.
好的...我知道这是旧线程,但我想确保访问此线程的工程师有最新信息。
Starting from Android O - As part of Background Check Limits ( Implicit receivers in this case ), The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect on your app, because it is now a private, implicit broadcast.
从 Android O 开始 - 作为背景检查限制的一部分(在这种情况下为隐式接收器), com.android.launcher.action.INSTALL_SHORTCUT 广播不再对您的应用程序产生任何影响,因为它现在是一个私有的隐式广播。
Per Android O ActivityManagerService.java :
每个 Android O ActivityManagerService.java :
case "com.android.launcher.action.INSTALL_SHORTCUT":
// As of O, we no longer support this broadcasts, even for pre-O apps.
// Apps should now be using ShortcutManager.pinRequestShortcut().
Log.w(TAG, "Broadcast " + action
+ " no longer supported. It will not be delivered.");
I hope this helps !
我希望这有帮助 !