how to create a shortcut which leads to a non-launcher activity?

别说谁变了你拦得住时间么 提交于 2019-11-27 16:28:37

问题


I want to create a shortcut in an android app, it lead to another activity which is not launcher of the app.


回答1:


To create the shortcut itself you need a specially crafted activity which must:

  • Be defined in your AndroidManifest.xml with an intent filter with the action android.intent.action.CREATE_SHORTCUT.
  • Return a result, an Intent, containing your actual shortcut. The shortcut itself is represented by another Intent.

This activity will then show up when you longpress your desktop and select "Shortcuts".

Of course the shortcut by itself is not much use, so you must add an intent filter to whatever activity you want to get triggered by the shortcut. The intent filter should match whatever Intent you chose for your shortcut.

I wrote a small how-to on the subject, it's got more details: http://www.kind-kristiansen.no/2010/android-adding-desktop-shortcut-support-to-your-app/

Do tell me if anything is unclear in that post, I'll try to clear it up.




回答2:


I have developed one method below for creating the shortcut icon on android Homescreen. Just call it.

private void ShortcutIcon(){

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

Don't forget to change your activity name, icon resource . Happy coding !!!



来源:https://stackoverflow.com/questions/4679022/how-to-create-a-shortcut-which-leads-to-a-non-launcher-activity

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