Tôi sử dụng các phương pháp này để thêm hoặc xóa phím tắt đúng cách. Các phương pháp này hoạt động khá tốt và là giống như Hệ thống Android khi người dùng thêm/xóa phím tắt theo cách thủ công.
public static void addShortcut(Context context)
{
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
ApplicationInfo appInfo = context.getApplicationInfo();
// Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
shortcut.putExtra("duplicate", false); // Just create once
// Setup activity shoud be shortcut object
ComponentName component = new ComponentName(appInfo.packageName, appInfo.className);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));
// Set shortcut icon
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
context.sendBroadcast(shortcut);
}
public static void deleteShortcut(Context context)
{
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
ApplicationInfo appInfo = context.getApplicationInfo();
// Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
context.sendBroadcast(shortcut);
}
Quyền:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
BroadcastReceiver:
<receiver android:name="YOUR.PACKAGE.PackageReplacedReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" android:path="YOUR.PACKAGE" />
</intent-filter>
</receiver>
Bạn không nghĩ điều này có thể là hành vi xấu? Nếu người dùng muốn biểu tượng trên màn hình của họ, họ có thể tự làm rất dễ dàng. – Jems
Điều này tương tự như những ứng dụng Windows gây hấn mà đòi hỏi phải tự thêm vào màn hình trên mỗi lần cài đặt/nâng cấp/khởi động. Ngay cả khi bạn tìm thấy một cách, * xin vui lòng không làm điều đó *. – ZoogieZork
@Jems, ZoogieZork cả hai đều chính xác, nhưng một số trường hợp sẽ yêu cầu chức năng này, ví dụ: cửa hàng ứng dụng doanh nghiệp chỉ khả dụng cho nhân viên trên thiết bị của công ty. Rất nhiều người dùng này sẽ không có kiến thức để đưa các ứng dụng lên màn hình chính, nhưng sẽ có trải nghiệm người dùng tốt hơn nhiều nếu chúng tôi làm điều đó cho họ. – Kennifer