Android - Create shortcut from adb

余生长醉 提交于 2020-01-02 08:10:30

问题


I want to create a shortcut via adb for an application I'm developing.

I have looked in Grepcode to see how the intent Android is expecting to be formatted. I also checked the AM documentation to try to create the intent required.

So far I got a lot of variations of the line below, but this one seems the most appropriate.

adb -d shell am broadcast \
-a com.android.launcher.action.INSTALL_SHORTCUT \
--es Intent.EXTRA_SHORTCUT_NAME "<shortcut-name>" \
--esn Intent.EXTRA_SHORTCUT_ICON_RESOURCE \
<package-name>/.activity

I have left the EXTRA_SHORTCUT_ICON_RESOURCE null, since Android should search the package itself and use the app_icon defined there.

The command runs and produces

Broadcasting: Intent { act=com.android.launcher.action.INSTALL_SHORTCUT cmp=<package-name>/.activity (has extras) }
Broadcast completed: result=0

No shortcut is added to the main screen, and I suppose it has something to do with the switches of the command.

Anyone has come across anything like this?

Any help greatly appreciated.

-JK


回答1:


Taking the default ICS Launcher as reference, I see that the intent expects an extra which you are not sending: android.intent.extra.shortcut.INTENT. This intent is the one that will be used to start the application pointed by shortcut. Realize that the type expected for this extra is a parcelable . So far I know, am is not able to sent such data structure.

As a workaround, you can create a very simple application to send this broadcast.

First of all, add the permission <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> to AndroidManifest.xml

Then, you could try in activity:

Intent shortcut = new Intent(Intent.ACTION_VIEW);
shortcut.setClassName("<package-name>", "<package-name>.activity");

Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "<shortcut-name>");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);

sendBroadcast(intent);


来源:https://stackoverflow.com/questions/21043432/android-create-shortcut-from-adb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!