[英]how to create app shortcut on home screen on Android phones on installation
I have created an android application , which works fine but now i want to add a feature that on installation the application should create a shortcut on home screen 我已经创建了一个android应用程序,它可以正常工作,但是现在我想添加一个功能,即在安装时该应用程序应在主屏幕上创建一个快捷方式
please suggest i don't want the complete code work simple steps would be enough 请建议我不要完整的代码工作,简单的步骤就足够了
First declare that your application is using the INSTALL_SHORTCUT permission in the AndroidManifest.xml. 首先,在AndroidManifest.xml中声明您的应用程序正在使用INSTALL_SHORTCUT权限。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(addIntent);
in your manifest.. 在你的清单中..
<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result. 然后,在接收该意图的活动中,为快捷方式创建一个意图,并将其作为活动结果返回。
// create shortcut if requested
ShortcutIconResource icon =
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intent = new Intent();
Intent launchIntent = new Intent(this,ActivityToLaunch.class);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
setResult(RESULT_OK, intent);
Tested Try it: 经过测试尝试:
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.setClassName(this, this.getClass().getName());
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
getApplicationContext().sendBroadcast(intent);
Good Luck... 祝好运...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.