+3

Shortcut cho ứng dụng android

Có thể vấn đề này không quá quan trọng, vì không có shortcut thì vào Home -> App -> chọn cũng được, nhưng mà mình có động đến nên cũng muốn chia sẻ một chút.

1. Shortcut icon cho app của mình

1.1. Tạo shortcut Đầu tiên thì cần phải khai báo quyền INSTALL_SHORTCUT ở trong Manifest

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

Sau đó tạo shortcut cho app bằng cách

        Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        Intent shortcutIntent = new Intent(this, MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test 123");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher));
        sendBroadcast(intent);
  • Ở đây chú ý là phần EXTRA_SHORTCUT_INTENT, thì intent sau đó là intent để gọi đến Launch Activity (trong trường hợp này là MainActivity)
  • Dòng shortcutIntent.setAction(Intent.ACTION_MAIN); cần thiết để có thể remove được shortcut
  • Phần EXTRA_SHORTCUT_NAME là name sẽ hiển thị bên dưới app icon
  • EXTRA_SHORTCUT_NAME sẽ là app icon
  • Ngoài ra chỉ cần biết tên package cũng tạo được luôn shortcutIntent, ko phải quan tâm đến setAction (xem phần 2. Shortcut icon đối với các app khác) Kết quả sẽ có 1 icon xuất hiện ngoài màn hình:

1.2. Xóa shortcut đã tạo Khai báo quyền UNINSTALL_SHORTCUT ở trong Manifest

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

Để remove shortcut đã tạo thì cần một số điều kiện là shortcutIntent khi tạo ở 1.1 có setAction(Intent.ACTION_MAIN), và EXTRA_SHORTCUT_NAME phải giống nhau

        Intent addIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
        Intent shortcutIntent = new Intent(this, MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test 123");
        sendBroadcast(addIntent);

2. Shortcut icon đối với các app khác

Thường thì sau khi install 1 app từ PlayStore thì bạn sẽ thấy nó tự tạo ra 1 cái shortcut, như vậy là mình cũng có thể tạo được icon cho ứng dụng khác. Cũng khá là tương tự như phần 1, ngoài một số điều cần lưu ý là

  • Cần phải biết package name của app, để get được Lauch Intent của app đó
  • Để lấy resource ở bên trong app khác thì phải dùng hàm packageManager.getResourcesForApplication(..), sẽ trả về 1 Resource cho phép truy cập đến resource của app khác
  • Extra put vào EXTRA_SHORTCUT_ICON_RESOURCE là một ShortcutIconResource, nhưng nếu muốn lấy icon của app đó, giống nhưu thèn playstore làm thì không thể khởi tạo như phần 1, cần phải setResourceName cho nó, lấy từ icon resId của app đó
  • Vì mình làm y hệt như playstore nên sau khi install 1 app -> Playstore nó tạo ra icon thì cũng có thể remove được icon đó
  • Còn một phần là icon tạo ra bằng cách longpress vào appicon rồi kéo thả ra màn hình home thì ko remove được, chắc nó do lancher tạo ra và quản lý (cái này mình ko rõ)
  • Ví dụ với Temple Run 2
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String testPackageName = "com.imangi.templerun2";
        findViewById(R.id.buttonAddShortcut).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addShortcut(testPackageName);
            }
        });
        findViewById(R.id.buttonRemoveShortcut).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeShortcut(testPackageName);
            }
        });
    }

    private void addShortcut(String packageName) {
        PackageManager packageManager = getPackageManager();
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
            Resources resourcesForApplication =
                packageManager.getResourcesForApplication(applicationInfo);
            String resourceName = resourcesForApplication.getResourceName(applicationInfo.icon);
            Intent launchIntentForPackage = packageManager.getLaunchIntentForPackage(packageName);
            Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getShortcutNameFromPackage(packageName));
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntentForPackage);
            Intent.ShortcutIconResource shortcutIconResource = new Intent.ShortcutIconResource();
            shortcutIconResource.packageName = packageName;
            shortcutIconResource.resourceName = resourceName;
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIconResource);
            addIntent.putExtra("duplicate", false);
            sendBroadcast(addIntent);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void removeShortcut(String packageName) {
        Intent shortcutIntent =
            getPackageManager().getLaunchIntentForPackage(packageName);
        Intent removeIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
        removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getShortcutNameFromPackage(packageName));
        sendBroadcast(removeIntent);
    }

    private CharSequence getShortcutNameFromPackage(String packageName) {
        PackageManager packageManager = getPackageManager();
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
            Resources resourcesForApplication =
                packageManager.getResourcesForApplication(applicationInfo);
            Intent launchIntentForPackage = packageManager.getLaunchIntentForPackage(packageName);
            int i =
                packageManager.getActivityInfo(launchIntentForPackage.getComponent(), 0).labelRes;
            return i != 0 ? resourcesForApplication.getString(i) :
                packageManager.getApplicationLabel(applicationInfo);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
  • Kết quả:

Thank you for reading !


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí