分享

Android: Is there a programming way to create a web shortcut on home screen

 quasiceo 2015-10-12

Do you know is there a programming way to create a web shortcut on the phone user's home screen?

What I want to do is:

When the phone user click a button in our apk application, the application will then place a website shortcut onto the phone user's home screen

Thanks a lot

ty

asked Apr 15 '11 at 11:36
ty.
339514

2 Answers

up vote 22 down vote accepted

First you'll need to add a permission to your manifest.xml

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

You'll need to build an intent to view the webpage. Something like...

Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www."));

You can test this by creating a little test app and doing startActivity(i); This should open the browser. Once you verified that the above intent is correct you should move on to the next step.

Now you'll need to actually install the shortcut.

    Intent installer = new Intent();
    installer.putExtra("android.intent.extra.shortcut.INTENT", i);
    installer.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
    installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", I THINK this is a bitmap); //can also be ignored too
    installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(installer)

;

It's also possible some home screens don't accept this, but most do. So enjoy.

EDIT: Icon can be set to the shortcut using:

installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon));
answered Apr 15 '11 at 12:31
Mike dg
3,56111017

    
hi Mike, I really appreciate your answer, let me try if this works in my app, and will get back to you if I have any question – ty. Apr 16 '11 at 17:07
    
hi Mike, your solution rocks! many thanks! One more question: is there anyway to check if any shortcut of target url has already been placed on the user's home screen. The situation is: we have a few apks. each of them will place a shortcut to our online apk store. so before placing a shortcut, I wish to check if one is already exists. Many thanks – ty. Apr 19 '11 at 3:19
1  
ty, not that I am aware of. Sorry. – Mike dg Apr 19 '11 at 12:40
1  
This technique is not recommended. This is an internal implementation, not part of the Android SDK. It will not work on all home screen implementations. It may not work on all past versions of Android. It may not work in future versions of Android, as Google is not obligated to maintain internal undocumented interfaces. Please do not use this. – CommonsWare Jun 13 '11 at 23:58
1  
It's an intent, nothing happens if there isn't anything that can accept it, it's the same as using any third party app. Sometimes something can receive that broadcast, other times it can't. – Mike dg Jun 14 '11 at 13:10

You can't place UI elements on the phone's home screen through an .apk. For the web shortcut - you can create a widget that opens the browser (with the specified URL) upon click.



    private void addShortCut(String tName ,String url) {
        // 安装的Intent 
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

        // 快捷名称 
        //tName="xxxx";
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, tName);
        // 快捷图标是允许重复
        shortcut.putExtra("duplicate", false);

        Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.putExtra("tName", tName);
       
        Uri uri=Uri.parse(url);
        shortcutIntent.setData(uri); 
        shortcutIntent.setAction(Intent.ACTION_VIEW);
        //shortcutIntent.setClassName("com.qqyumidi.shortcutdemo", "com.qqyumidi.shortcutdemo.AppStart");
        shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

       
        // 快捷图标 
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

        // 发送广播 
        sendBroadcast(shortcut);
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多