【发布时间】:2014-03-24 15:13:33
【问题描述】:
调用此代码以简单地在主屏幕中创建快捷方式时,我得到了一个奇怪的结果。
快捷方式创建在主屏幕的第二页(第一页是空的,所以有足够的空间!)。有什么想法吗?
public static void installShortcut(Context context, String packageName, String componentName, String shortcutName, Parcelable icon) {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
ComponentName cn = new ComponentName(packageName, componentName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(cn));
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcut.putExtra("duplicate", false);
context.sendBroadcast(shortcut);
}
// gets some info from external package by name
public static void createShortcutForPackage(Context context, String packageName, String className) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, className));
PackageManager pm = context.getPackageManager();
ResolveInfo ri = pm.resolveActivity(intent, 0);
String shortcutName = ri.loadLabel(pm).toString();
String activityName = ri.activityInfo.name;
int iconId = ri.activityInfo.applicationInfo.icon;
Context pkgContext;
try {
pkgContext = context.createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
if (pkgContext != null) {
ShortcutIconResource sir = Intent.ShortcutIconResource.fromContext(pkgContext, iconId);
installShortcut(pkgContext, packageName, activityName, shortcutName, sir);
}
} catch (NameNotFoundException e) {
}
}
这是默认的 Android 4.2.2 主屏幕:
更新: 在 Android 4.0.4 上,快捷方式是在正确的位置创建的。
【问题讨论】:
-
第二页是默认页吗?
-
我点击“主页按钮”然后滑动聚焦下一页,这是第二页(在主页的右侧)
-
所以将默认页面更改为第一页而不是第二页
-
我有点迷茫,怎么办?
-
是的,您使用的是哪个启动器?
标签: android android-intent shortcut android-4.2-jelly-bean homescreen