简体   繁体   中英

Android: How can I launch shortcut of a certain app in my own application?

I'm developing some kinds of beginner-level apps, and wondering if it is possible to launch a certain shortcut (not the app itself) in my own application.

Actually, I found the way that launch apps with their package names, but what I need to do is to start some shortcuts (which is located in my homescreen) when I touch the special button in my app.

Is there a way to do this?

May be useful, but how to get classes from manifest, I'd like to know http://www.tutorialforandroid.com/2009/10/launching-other-application-using-code.html

Say you want to open fuelgauge, to do this.

final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.settings","com.android.settings.fuelgauge.PowerUsageSummary");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);

Explanation To open other people's application, you need to make sure that in their manifest file, the author specify the class to have the android.intent.action.MAIN intent-filter added to them.

final Intent intent = new Intent(Intent.ACTION_MAIN, null);

We then add category that this new intent will be launching something

intent.addCategory(Intent.CATEGORY_LAUNCHER);

Then we get identify the application we need to open by using ComponentName, here you specify the package name of the application as first argument and the class we want to open as the second one. You must understand that com.android.settings has a lot of classes that have Main intent-filter under it making the second argument to be the specific class that we need. (this is more than one line)

final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.fuelgauge.PowerUsageSummary");

After we identify the component we want, we set it to our intent

intent.setComponent(cn);

We then tell the intent that open opening this one make it as a new task

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Then finally start our intent

startActivity( intent);
public static void createShortcut(@NonNull Activity activity, Class activityToOpen, String title,   int icon) {
    Intent shortcutIntent = new Intent(activity, activityToOpen);
    shortcutIntent.setAction("android.intent.category.LAUNCHER");
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // code for adding shortcut on pre oreo device
        Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
        intent.putExtra("duplicate", false);
       // Parcelable parcelable = Intent.ShortcutIconResource.fromContext(activity, icon);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  icon);
        activity.sendBroadcast(intent);
        //System.out.println("added_to_homescreen");
    } else {
        ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
        assert shortcutManager != null;
        if (shortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo pinShortcutInfo =
                    new ShortcutInfo.Builder(activity, "idhere")
                            .setIntent(shortcutIntent)
                            .setIcon(Icon.createWithResource(activity, icon))
                            .setShortLabel(title)
                            .build();

            shortcutManager.requestPinShortcut(pinShortcutInfo, null);
            System.out.println("added_to_homescreen");
        } else {
            System.out.println("failed_to_add");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM