Android-Add a URL icon to the mobile desktop (solution 2)
The previous article mainly opened the app to enable the website. Although it is relatively simple to implement, the effect is still flawed.
This article uses the Broadcast mechanism to implement the desktop icon link URL function, which not only works well, but also has the biggest advantage that you no longer need to use the app to open the website.
The implementation steps are as follows:
1. add permissions to the AndroidManifest. xml configuration file:
2. Set and add the broadcast listening Intent in the OnCreate method of MainActivity:
Final Intent shortCutIntent = new Intent ("com. android. launcher. action. INSTALL_SHORTCUT "); final Parcelable icon = (BitmapDrawable) context. getResources (). getDrawable (R. drawable. ic_launcher )). getBitmap (); // retrieve the icon Uri uri of the shortcut key = Uri. parse ("http://blog.csdn.net/wanggsx918"); Intent pendingIntent = new Intent (Intent. ACTION_VIEW, uri); // The shortCutIntent icon on the desktop. putExtra (Intent. EXTRA_SHORTCUT_ICON, icon); // desktop shortcut title shortCutIntent. putExtra (Intent. EXTRA_SHORTCUT_NAME, context. getString (R. string. app_name); // desktop shortcut Action: The action shortCutIntent when the icon is clicked. putExtra (Intent. EXTRA_SHORTCUT_INTENT, pendingIntent); context. sendBroadcast (shortCutIntent );
Yes, this implementation method is both authentic and perfect. This is the taste!