Depending on the needs of the app, there are widgets in the app that embed the widget code so that we can select the app widget you want to add by long-pressing the phone's screen-to-widget. This can give users a better and faster way to use the experience, this is the actual meaning of Android desktop widgets.
It is an example of a desktop widget of the Youdao dictionary (another red icon is a simple desktop widget for the Sun app):
Appwidgetprovider is an Android-provided class for implementing desktop widgets, which is essentially a broadcast. The Remoteviews class is also used, which is a cross-process remote class that uses two types of scenarios: Notification bar and Widget.
Development Steps for Widgets:
1. Define Widget interface
Res/layout directory to create a new XML layout file, here is Widget.xml, layout is simple is a red icon
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <imageview android:id= "@+id/imageview1" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:layout_gravity=" Center_horizontal " android: src= "@drawable/sun"/></linearlayout>
2. Define the widget configuration information:
Create a new XML layout file under the Res/xml directory, this is New_app_widget_info.xml
<?xml version= "1.0" encoding= "Utf-8"? ><appwidget-provider xmlns:android= "http://schemas.android.com/apk /res/android "android:initialkeyguardlayout=" @layout/widget "android:initiallayout=" @layout/widget "android:minHe ight= "40DP" android:minwidth= "40DP" android:previewimage= "@drawable/example_appwidget_preview" Android:resizemode = "Horizontal|vertical" android:updateperiodmillis= "86400000" android:widgetcategory= "Home_screen" ></ Appwidget-provider>
3. Define the implementation class for the widget:
Package Xml;import Android.app.pendingintent;import Android.appwidget.appwidgetmanager;import Android.appwidget.appwidgetprovider;import Android.content.componentname;import Android.content.Context;import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.matrix;import Android.os.systemclock;import Android.widget.remoteviews;import Com.hongri.recyclerview.r;import com.hongri.recyclerview.utils.logger;/** * Desktop Widget implementation class. */public class Newappwidget extends Appwidgetprovider {private static final String click_action = "Android.appwidget.a Ction.click "; private static Remoteviews remoteviews; Public Newappwidget () {super (); /** * Desktop Widget Update * @param context * @param appwidgetmanager * @param appwidgetid */static void U Pdateappwidget (context context, Appwidgetmanager Appwidgetmanager, int appwidgetid) { LOGGER.D ("Updateappwidget"); SendpendiNgintentcast (context); Appwidgetmanager.updateappwidget (Appwidgetid, remoteviews); } @Override public void OnReceive (final context context, Intent Intent) {super.onreceive (context, Intent); if (Intent.getaction (). Equals (Click_action)) {LOGGER.D ("onreceive--click_action"); New Thread (New Runnable () {@Override public void run () {Bitmap srcbbitm AP = Bitmapfactory.decoderesource (Context.getresources (), R.drawable.sun); Appwidgetmanager Appwidgetmanager = appwidgetmanager.getinstance (context); for (int i = 0; i < PNS; i++) {Float degree = (i*10)% 360; Sendpendingintentcast (context); Remoteviews.setimageviewbitmap (R.id.imageview1,rotatebitmap (Context,srcbbitmap,degree)); Appwidgetmanager.updateappwidget (New ComponentName (Context,newappwidget.class), RemoteViews); Systemclock.sleep (30); }}). Start (); }; }/** Set Rotation animation */Private Bitmap Rotatebitmap (context context, Bitmap Srcbbitmap, float degree) {matrix matrix = new Matrix (); Matrix.reset (); Matrix.setrotate (degree); Bitmap Tmpbitmap = Bitmap.createbitmap (Srcbbitmap,0,0,srcbbitmap.getwidth (), Srcbbitmap.getheight (), matrix,true); return tmpbitmap; }/** * "Desktop widget" click Send Intent Broadcast * @param context */private static void Sendpendingintentcast (Context conte XT) {remoteviews = new Remoteviews (Context.getpackagename (), r.layout.widget); Intent Intentclick = new Intent (); Intentclick.setaction (click_action); Pendingintent pendingintent = Pendingintent.getbroadcast (context,0,intentclick,0); Remoteviews.setonclickpendingintent (r.id.imageview1,pendingintent); /** * This method will be called every time the desktop widget is updated * @param context * @param appWidgetmanager * @param appwidgetids */@Override public void onUpdate (context context, Appwidgetmanager APPW Idgetmanager, int[] appwidgetids) {LOGGER.D ("onUpdate"); for (int appwidgetid:appwidgetids) {updateappwidget (context, Appwidgetmanager, Appwidgetid); } }}
4. The Androidmanifest.xml file declares the widget (the mechanism of the widget is broadcast, so it must be declared):
<receiver android:name= "XML. Newappwidget "> <intent-filter> <action android:name=" Android.appwidget.action.click " ></action> <action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/> &L t;/intent-filter> <meta-data android:name= "Android.appwidget.provider" Android Oid:resource= "@xml/new_app_widget_info"/></RECEIVER>
finally run the program no problem, You can add widgets to the desktop:
Press and hold the desktop--Enter widget--Red Sun app--> Store this widget under the app
Apps for Android desktop Widgets (appwidgetprovider)