Android Simple Combat Tutorial--the 14th, "imitation Jinshan Assistant to create desktop widget widget"

Source: Internet
Author: User
Tags home screen

Open the Google API and make the following description of the widget widget:

APP Widgets is miniature application views that can is embedded in otherapplications (such as the Home screen) and receive Periodic updates. These views arereferred to as Widgets in the user interface,and you can publish one and an App Widget provider. An application componentthat is able-to-hold other app Widgets are called an app Widget host. The Screenshotbelow showsthe Music App widget. Also, for example, the most commonly used widgets are music player widgets:

The documentation gives a very detailed use of the method. The following are the specific actions:

First, declare the AppWidgetProvider class in yourapplication ' s AndroidManifest.xml file. For example:

<receiver android:name= "Exampleappwidgetprovider" >    <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>

So tiger, we create a demo in our own application to make this widget:

New project, create a more class: public class Exampleappwidgetprovider extends appwidgetprovider{} It's a special broadcast recipient.

The above code, all copied to their own manifest file, found android:resource= "@xml/example_appwidget_info" will be error, because the project does not have this reference, so create an XML folder, and then create Example_ Appwidget_info This XML file, the content inside, continue to look at the API documentation:

Next you see this line of text:

Adding the Appwidgetproviderinfo Metada

The following code is immediately below:

<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" ></appwidget-provider>

We need to copy it into the Example_appwidget_info file we just created.

After copying the past, you will find two lines of code error:

Android:previewimage= "@drawable/preview"
android:initiallayout= "@layout/example_appwidget"

Decibels are layouts, as well as pictures. Our project obviously does not have these two resources, so we need to create both. And in order to imitate Jinshan Guardian which small parts, I went directly to copy the resources of Jinshan Guardian.

File name is: Process_widget_provider

Copy all the contents into the Example_appwidget_info file just now.

<?xml version= "1.0" encoding= "Utf-8"? ><appwidget-provider xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:initiallayout=" @layout/process_widget "    android:minheight=" 72.0dip "    android: Minwidth= "294.0dip"    android:updateperiodmillis= "0"/>

Of course, not yet introduced Initiallayout, this line of code must also be an error. So we're going to join this layout resource.

Jinshan Assistant's res layout file has process_widget files, all copied to their own project inside

This way, the last file was not wrong, and the new file error came again. Do not worry, the basic is no resources to error, as long as the introduction of the appropriate resources to be good.

In the source code of Jinshan, assign the resource reference of the error. Finally, the good code is as follows: (no need to understand all, but to know how to assign value paste, know the process of production)

Believe that you can exclude these errors by referencing the resources. Now it's time to run--

You can now drag it directly to the desktop to show it. As follows:


In this way, the widget has been created! The imitation show was done.

So, how do you add data to this small interface? Before we had a class, public class Exampleappwidgetprovider extends appwidgetprovider{} It was a special broadcast recipient

There is no code logic for it, in fact, the broadcast receiver is associated with the widget. Add the following methods at a glance. Now it's time to write the code here:

First, experience the life cycle of some widgit. Override some of the following methods:

public class Exampleappwidgetprovider extends Appwidgetprovider {@Overridepublic void ondeleted (context context, int[] Appwidgetids) {//TODO auto-generated Method stubsuper.ondeleted (context, appwidgetids); System.out.println ("ondeleted");} @Overridepublic void ondisabled (Context context) {//TODO auto-generated Method stubsuper.ondisabled (context); System.out.println ("ondisabled");} @Overridepublic void onenabled (Context context) {//TODO auto-generated Method stubsuper.onenabled (context); System.out.println ("onenabled");} @Overridepublic void OnReceive (context context, Intent Intent) {//TODO auto-generated method Stubsuper.onreceive ( context, intent); System.out.println ("OnReceive");} @Overridepublic void OnUpdate (context context, Appwidgetmanager appwidgetmanager,int[] appwidgetids) {//TODO Auto-generated method Stubsuper.onupdate (context, Appwidgetmanager, appwidgetids); System.out.println ("OnUpdate");}}

Run and create the widget part here. Take a look at the log output:

Join to create two small controls:

See two more prints. Re-create-

Of course, the output of the upper red line is executed.

Now delete a part, print out:

ondeleted

OnReceive

Continue to delete the last one:

ondeleted

OnReceive

Ondisabled

OnReceive

Can be summed up: ondisabled and onenabled are only executed once. Then these two methods are the key method; one is executed at creation time, and one is executed when destroyed. Some of your own logic code is written in these two methods.

As for what logic to write, according to their own needs to decide. In this way, the widget widgets are generated.









Android Simple Combat Tutorial--the 14th, "imitation Jinshan Assistant to create desktop widget widget"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.