Android Widget Development Example: the implementation of desktop sticky note program specific explanation and source code (above)

Source: Internet
Author: User

If there are mistakes please do not hesitate to make bricks, reproduced please indicate the source, thank you

Desktop note Software is one of the most frequently used software on Android, such as the earlier Sticky Note, which was popular,

Sticky Note's introduction can be seen http://www.tompda.com/c/article/11778/

In fact, using the Android platform for Widget development support, desktop sticky note software is very easy to develop.

This article through the gradual realization of a simple desktop note software, and everyone to share the process and methods of the development of the widget.

1.MyNote finally achieves the effect

In order to mention the interest in reading this article, let us introduce the effect of the realization finally.

The first is the ability to add our MyNote widgets through the desktop, for example, as seen in:

The "My Notes" in the diagram is the sticky note program we will be developing later.

Click to start the Join log interface, for example as seen in:

After entering the content of the note, you can click on one of the four icons listed below as a note icon.

For example, after clicking on the first one, a note will be added to the desktop:

Click on the note on the desktop to make changes to the note content again and change the icon.

The desktop can have multiple notes at the same time and can be changed separately.

For example, as you can see, we've changed the icon for the note we just created and added a note:

The contents of each note are saved independently and can be changed at any time by clicking on the desktop icon.

2. Development methods

The purpose of development and the effect of the pursuit has been very clear, first of all we determine the development method.

In this article, we will take a progressive development, that is to say, do not start from the beginning to the end.

But it is divided into several stages. Each phase is finished with a certain goal, and then the next phase adds a lot of other features,

Each phase is a step closer to the goal, OK, you can say this is an agile development:)

In the first stage, we'll start by building a widget prototype program,

It is fully executable and able to create desktop widgets.

In the second phase, we improved the implementation of the Widget Configuration activity section

Make it a feature to create notes

In the third phase, we improved the implementation of the widget click Response section,

Make it a feature to change notes

3. Building Widget Prototype Program

In this section we will make a prototype of the simplest widget program, but it can be executed.

In general, the widget program consists of the following sections:

A. Implementation of the Appwidgetprovider

B. Widget appearance layout definition file

C. Implementation of configuration activity when adding widgets (optional)

D. widget parameter configuration file

The following are explained separately

A. Implementation of the Appwidgetprovider

First we create a new androidproject named MyNote, and then change the Mynote.java code,

Make mynote inherit from Appwidgetprovider, and override the OnUpdate and Ondeleted methods.

The onupdate will be called when the widget is created and updated, and ondeleted will be called when the widget is deleted.

For now, we don't need to do any function here, just simply log the logs so that we can observe their execution, and write good code like the following:

Package Com.silenceburn;import Android.appwidget.appwidgetmanager;import android.appwidget.AppWidgetProvider; Import Android.content.context;import Android.util.log;public class MyNote extends Appwidgetprovider {/** Called when The activity is first created. */final String mperfname = "com.silenceburn.MyColorNoteConf"; @Overridepublic void OnUpdate (context context, Appwidgetmanager appwidgetmanager,int[] appwidgetids) {//TODO auto-generated method stubfinal int N = Appwidgetids.lengt h;for (int i = 0; i < N; i++) {int appwidgetid = appwidgetids[i]; LOG.I ("MyLog", "This is [" + Appwidgetid + "] onupdate!");}} @Overridepublic void ondeleted (context context, int[] appwidgetids) {//TODO auto-generated method stubfinal int N = APPWI dgetids.length;for (int i = 0; i < N; i++) {int appwidgetid = appwidgetids[i]; LOG.I ("MyLog", "This is [" + Appwidgetid + "] ondelete!"); }}}

B. Widget appearance layout definition file

We need to write an appearance layout file for the widget, in this demo sample, the layout is easy, just need a imageview to

Well-written my_note_widget.xml files such as the following:

<?xml version= "1.0" encoding= "Utf-8"? ><imageview xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/my_widget_img "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:src= "@drawable/sketchy_paper_008" android:clickable= "true"/>

Here is an external image sketchy_paper_008.png, from the web, thanks to the original author of the picture.

able to pack and download to http://dryicons.com/free-icons/preview/sketchy-paper-icons/.

(Note that the file names in the downloaded package may differ from those in the program I wrote, please be careful to adjust them yourself.) )

C. Implementation of configuration activity when adding widgets (optional)

The Android platform provides a configuration interface for widgets, and we can define an activity for ourselves,

Once the relevant parameters have been configured in the widget configuration file, the activity will be invoked by the user when the widget is added.

In general, the purpose of this configuration interface is to allow users to configure some properties of the widget, such as color, size, and so on, when a user creates a new widget.

But in our Demo sample program, we use it as a place to create notes!

Just this section is just to implement a prototype program, so temporarily do not do the processing, we just create a new activity.

New activity named Mynoteconf, overriding the OnCreate method, in the OnCreate method,

Because this activity is called by the system when the new widget is added,

So we can use getintent to get the incoming widgetid. Can infer whether it is a valid Widgetid,

Finally we must return a RESULT_OK intent, and end the current activity, the system will feel the configuration is successful, put this widget on the desktop.

Assuming that the result_canceled is returned, the system feels the configuration fails and terminates the widget creation process.

Write the code for mynoteconf such as the following:

Package Com.silenceburn;import Android.app.activity;import Android.appwidget.appwidgetmanager;import Android.content.intent;import Android.os.bundle;import Android.util.log;public class MyNoteConf extends Activity {int Mappwidgetid; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); LOG.I ("MyLog", "on widgetconf ... "); Setresult (result_canceled);//Find the widget ID from the intent. Intent Intent = Getintent (); Bundle extras = Intent.getextras (); if (extras! = null) {Mappwidgetid = Extras.getint (appwidgetmanager.extra_appwidget_ ID,APPWIDGETMANAGER.INVALID_APPWIDGET_ID);} If they gave us an intent without the widget ID, just bail.if (Mappwidgetid = = appwidgetmanager.invalid_appwidget_id) { Finish ();} return okintent Resultvalue = new Intent (); Resultvalue.putextra (Appwidgetmanager.extra_appwidget_id,mappwidgetid) ; Setresult (RESULT_OK, Resultvalue); Finish ();}}

D. widget parameter configuration file

Finally, we need to write a widget parameter configuration file that associates the layout file with the configuration activity.

We create the new folder XML under Res, add the file My_note_widget.xml under the XML folder, write the following example:

<appwidget-provider xmlns:android= "http://schemas.android.com/apk/res/android" android:minwidth= "72DP" android:minheight= "72DP" android:updateperiodmillis= "86400000" android:initiallayout= "@layout/my_note_widget" Android:configure= "com.silenceburn.MyNoteConf" ></appwidget-provider>

The MinWidth minheight is used to specify the size of the widget, assuming we only occupy a lattice, known as 1x1,

The 72DP width is a best practice value recommended by the Android platform.

Then we use Initiallayout to correlate the layout file we've written,

Using configure to correlate our programmed configuration with activity:mynoteconf,

In addition, another parameter updateperiodmills specifies the refresh period for the widget,

From the point of view of power-saving, generally set this value is relatively large, assuming that the widget must do periodic things, can use Alarmmanager.

Now that all the widgets are ready, let's take a look.

4. Implementing the Widget Prototyping program

To implement the widget, we also need to change the androidmanifest.xml to declare our widgets.

Declare a receiver, filter android.appwidget.action.APPWIDGET_UPDATE,

And it's associated with metadata to our own Appwidgetprovider implementations.

Declare an activity associated to our configuration class mynoteconf, filter Android.appwidget.action.APPWIDGET_CONFIGURE.

Finally, change the app icon, which appears in the list of new widgets for today's system.

Well-written androidmanifest.xml such as the following:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.silenceburn "android:versioncode=" 1 "android:versionname=" 1.0 "><application android: icon= "@drawable/sketchy_paper_008" android:label= "@string/app_name" ><receiver android:name= ". MyNote "><intent-filter><action android:name=" Android.appwidget.action.APPWIDGET_UPDATE "/></ Intent-filter><meta-data android:name= "Android.appwidget.provider" android:resource= "@xml/my_note_widget"/ ></receiver><activity android:name= ". mynoteconf "><intent-filter><action android:name=" Android.appwidget.action.APPWIDGET_CONFIGURE "/> </intent-filter></activity></application></manifest>

At this point the prototype program all the development is complete, take a look at the effect!

Long on the desktop, you can choose the prototype widget we just wrote "MyNote",

After selection, we define the configuration interface mynoteconf,

But because we finish in the OnCreate, so it is a flash.

Then mynote out on the table now.

We can drag it casually, or throw it in the trash, and look at the log output.

Upper Part Summary

The upper part of the main finished a widget prototype, it does not matter what business functions,

But it's already a skeleton that can be executed.

In the lower part we add blood and meat to it, so that it really has a business function.

I hope you like this first written skeleton, and then gradually enrich the development of the way:)

The second half has been baked Android Widget Development example: desktop sticky note Program implementation specific explanation and source code (below)

Address is: http://blog.csdn.net/silenceburn/archive/2010/12/23/6094705.aspx

Android Widget Development Example: the implementation of desktop sticky note program specific explanation and source code (above)

Related Article

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.