Long time no blog, do not know how to write blog, and recently suddenly want to write a blog to help more people, but do not know what to write good?
Well I admit I'm a little lazy, but the program ape should not be lazy oh, I want to be a good boy.
Well, let's start by introducing the main contents of today!
Widgets a small widget for a desktop personally, I think it is very common, do not know the gods do not think so? For example, you develop a music player software, you can put the basic previous and next song, pause a few functions in the widget to display it on the desktop, so it is very convenient, you want to the next song, a song, pause playback, you do not have to open the player, But it can be achieved directly through this small control, is not very practical? All right, here's how to use this beautiful little widget!
First, let's take a look at the official documentation:
On the homepage of the official document
- Develop----->API Guides----> left sidebar (App Widgets) to see the introduction
If you want to know more information about the widgets, you can go to the official documents to see, here do not do a detailed introduction, the following start how to write, is the most important.
First step: Write a class inheritance Appwidgetprovider
public class Mywidgets extends Appwidgetprovider {}
Then we have a question.AppwidgetproviderWhat the hell is that? Indeed, I had this question at the beginning of the day, and the following will be explained;
Let's take a look at the source code in Appwidgetprovider
public class Appwidgetprovider extends Broadcastreceiver { /** * Constructor to initialize appwidgetprovider.< c9/>*/public Appwidgetprovider () { }
Part of the source code is omitted here we look at the key part
Did you guys see that?
Appwidgetprovider is inherited Broadcastreceiver, and <pre name= "code" class= "Java" >broadcastreceiver is also a broadcast receiver, So what do you think?
The manifest file, of course, androidmanifest.xml.
The four components of Android need to be configured in the manifest file Androidmanifest.xml the next step is to configure the manifest file.
Step Two: Configure inheritance in Androidmanifest.xml in the manifest file Class of Appwidgetprovider
How exactly is it configured? The official Android documentation has given us the sample code, let's take a look
<receiver android:name= "Exampleappwidgetprovider" ><!--inherit the completed package name of the Appwidgetprovider class + class name-- < intent-filter><!--Intent Filter to tell you what this class does-- <action android:name= "android.appwidget.action.APPWIDGET_ UPDATE "/><!--Intent filter Action: Simply let you do what the class does-- </intent-filter> <!--metadata 1. Name 2. Referenced resources- <meta-data android:name= "Android.appwidget.provider" android:resource= "@xml/example_ Appwidget_info "/></receiver>
All right, here we have our manifest file configured, and it's important to note that:
is configured under the Application node in the manifest file , because it is the four components all need to be configured at the unified level
The location of the package name + class name in the above configuration can be modified according to its own package name and class name, followed by reference to the resources when we have to say a moment
go back to the Res folder of our program's structure to create a new XML file and create a new XML file within the folder the official file name is used here, and the reader can modify xml/example_appwidget_info.xml
But what? Here's a question, how exactly does this XML file be written? Don't worry, there are already "beauties" in the official Android docs waiting for us, waiting for us to hunt? Well, I think it's crooked, but I have to go see it. Hey, come on.
First of all we should know that since it is an XML file? You need to introduce a header file.
<?xml version= "1.0" encoding= "Utf-8"?>
And then we're going to look for "beautiful girls." Imagine that it's a beautiful woman. Would you be happy to see it?
<appwidget-provider xmlns:android= "http://schemas.android.com/apk/res/android" android:minwidth= "40DP" android:minheight= "40DP" android:updateperiodmillis= "86400000" android:previewimage= "@drawable/ Preview " android:initiallayout=" @layout/example_appwidget " android:configure=" Com.example.android.ExampleAppWidgetConfigure " android:resizemode=" horizontal|vertical " android: widgetcategory= "Home_screen|keyguard" android:initialkeyguardlayout= "@layout/example_keyguard" ></ Appwidget-provider>
Well, since the "beautiful" we have found, then we have to study it well.
Android:minwidth= "40DP" Desktop control default width android:minheight= "40DP" Desktop control default height <pre name= "code" class= "HTML" > <span style= "FONT-SIZE:14PX;" ></span><pre name= "code" class= "HTML" >android:updateperiodmillis= "86400000" The Shortest update time Here is the millisecond value default is 30 minutes
Android:previewimage= "@drawable/preview" preview picture If you do not specify a picture, the application's picture will be used by default <span style= "FONT-SIZE:14PX;" ></span><pre name= "code" class= "HTML" >android:initiallayout= "@layout/example_appwidget" layout of the desktop control is the layout we want to show <span style= "FONT-SIZE:14PX;" ></span><pre name= "code" class= "HTML" >android:configure= " Com.example.android.ExampleAppWidgetConfigure "If your application does not have an interface, you will need to specify the appropriate parameters for the interface program. <span style=" Font-size : 14px; " ></span><pre name= "code" class= "HTML" >android:resizemode= "horizontal|vertical" compression method Default horizontal and vertical compression
android:widgetcategory= "Home_screen|keyguard" <span class= "HIGH-LIGHT-BG" > declares if your app controls can be displayed on the screen </span> <span>.</span><p class= "Ordinary-output target-output" ><span class= "HIGH-LIGHT-BG" ></ Span><pre name= "code" class= "HTML" >android:initialkeyguardlayout= "@layout/example_keyguard" Component layout When you lock the screen well , here we have the resource files are finished, we need to remind you that in the development we do not need to write all the only need to write the corresponding properties according to our needs
In this step our basic desktop widgets have been set up, if you have more needs, please refer to the official Android documentation available.
Here to provide you with a simple demo
First step: Write a class inheritance Appwidgetprovider
public class Mywidgets extends Appwidgetprovider {private static final String TAG = "Mywidgets"; @Overridepublic void Ondis Abled (Context context) {super.ondisabled (context); LOG.I (TAG, "ondisabled");//Desktop No controls stop service Intent services = new Intent (context, widgetsupdateservice.class); Context.stopservice (service);} @Overridepublic void onenabled (context context) {super.onenabled (context); LOG.I (TAG, "onenabled");//desktop has control to open service update Intent services = new Intent (context, widgetsupdateservice.class); Context.startservice (service);} @Overridepublic void OnUpdate (context context, Appwidgetmanager appwidgetmanager,int[] appwidgetids) {super.onupdate ( Context, Appwidgetmanager, appwidgetids); LOG.I (TAG, "onUpdate");//Services Intent service = new Intent (context, widgetsupdateservice.class); Context.startservice ( Service);}
Step Two: Configure in the manifest file
Example_appwidget_info
<receiver android:name= "Com.androidit.receiver.MyWidgets" > <intent-filter> <action Android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/> </intent-filter> <meta-data android:name= "Android.appwidget.provider" android:resource= "@xml/example_appwidget_info"/> </receiver>
Create an XML folder under the Res resource folder, creating a new XML file (note Creating an XML folder before creating a Example_appwidget_info file)
<pre name= "code" class= "HTML" ><pre name= "code" class= "HTML" ><pre name= "code" class= "HTML" ><pre Name= "code" class= "HTML" ><pre name= "code" class= "HTML" ><span class= "HIGH-LIGHT-BG" >example_ Appwidget_info.xml</span><pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "Utf-8"?> <appwidget-provider xmlns:android= "http://schemas.android.com/apk/res/android" android:minwidth= "294DP" android:minheight= "72DP" android:updateperiodmillis= "0" android:resizemode= "horizontal|vertical" android:initiallayout= "@layout/process_widget" ></appwidget-provider>
Layout file Here is not attached, to everyone to see a final figure, as to what kind of layout everyone write it
If you are interested in this blog, please pay attention to Oh, do not regularly update the Android article