我想做的是:
1)我在一个活动中,有两个按钮。如果我单击第一个,就会在我的主屏幕中创建一个快捷方式。快捷方式打开一个以前已经下载的html
页面,所以我希望它使用默认浏览器,但我不想使用互联网,因为我已经有了这个页面。
2)第二个按钮创建另一个启动活动的快捷方式。并且我想向活动传递一些额外的参数(例如字符串).
这些都是可能的吗?我找到了一些链接和一些类似的问题,比如Android: Is there a programming way to create a web shortcut on home screen
他们似乎是我的问题的答案,但有人告诉我,这个代码不会在所有的设备上工作,这是不赞成的,我想做的事情是不可能的......
不推荐使用此技术。这是一个内部实现,不是Android SDK的一部分。它不会在所有的主屏幕实现上都起作用。它可能并不适用于所有过去版本的Android。它可能在未来的Android版本中不起作用,因为谷歌没有义务维护内部未记录的界面。请不要使用这个
什么是内部实现?代码是可信的还是not.....help me,请...
发布于 2011-06-14 07:42:33
示例代码使用未记录的接口(权限和意图)来安装快捷方式。正如“有人”告诉你的那样,这可能不会在所有手机上都起作用,可能会在未来的Android版本中中断。不要这样做。
正确的方法是侦听来自主屏幕的快捷方式请求--在您的清单中使用类似如下的意图过滤器:
<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>
然后,在接收意图的activity中,为快捷方式创建一个intent并将其作为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);
发布于 2013-12-12 13:54:32
我已经开发了下面的一种方法,用于在我自己的应用程序上测试的android主屏幕上创建快捷方式图标。就叫它吧。
private void ShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
别忘了更改你的活动名称,图标资源和权限。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
编码快乐!
编辑:
对于重复的问题,第一个选项是在代码中添加下面的行,否则每次都会创建一个新的。
addIntent.putExtra("duplicate", false);
第二个选项是先卸载App Shortcut图标,如果第一个选项不起作用,再安装一次。
发布于 2018-05-15 13:14:33
自android oreo以来,com.android.launcher.action.INSTALL_SHORTCUT广播不再有任何影响。LINK
如果你想支持所有的安卓版本,特别是安卓8.0或奥利奥和更新的,使用下面的代码来创建快捷方式:
public static void addShortcutToHomeScreen(Context context)
{
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
{
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
.setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
.setShortLabel("Test")
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}
else
{
// Shortcut is not supported by your launcher
}
}
在清单文件中添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
https://stackoverflow.com/questions/6337431
复制相似问题