Android learning notes (33)-appwidgetprovider and Update in widgets

Source: Internet
Author: User

For more information about appwidgetprovider, see the official documentation.

Recently, due to a problem, we used

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:initialLayout="@layout/widget"    android:minHeight="120dip"    android:minWidth="120dip"    android:updatePeriodMillis="5000" ></appwidget-provider>

The update cycle in Android: updateperiodmillis does not work. First, there are several reasons, which mean that the Android version is problematic. The other one is that it can only be updated once every 30 minutes on the Internet. Otherwise, the service will be used for updates. The following two methods are used:

I. Broadcast

1. Since Android: updateperiodmillis does not meet our requirements, we can use broadcast to update widgets according to the update cycle we want.

Let's take a look at the appwidgetprovider. Java file: (quasi-to my memo project: http://blog.csdn.net/moruna/article/details/7895817)

Public class appwidget extends appwidgetprovider {private databasehelper dbhelper; string [] pai_text; @ overridepublic void onupdate (context, appwidgetmanager, int [] appwidgetids) {// todo auto-generated method stubsuper. onupdate (context, appwidgetmanager, appwidgetids); // read the database record dbhelper = new databasehelper (context, "ideal. SQL "); sqlitedatabase DB = dbhelper. getreadabledatabase (); cursor = dB. query ("user", null, null); int partition _num = 0; // The desktop tab displays only 6 records; while (cursor. movetonext () {If (1__num = 6) {break;} string temp_text = cursor. getstring (cursor. getcolumnindex ("mtext"); // control the length of each record if (temp_text.length ()> 7) {temp_text = temp_text.substring (0, 7) + "... ";} pai_text [pai_num] = temp_text; system. out. println ("cmd_text [cmd_num]" + cmd_text [cmd_num]); cmd_num ++;} dB. close (); Final int num = appwidgetids. length; For (INT I = 0; I <num; I ++) {int [] mappwidgetid = appwidgetids; remoteviews mremoteviews = new remoteviews (context. getpackagename (), R. layout. widget); mremoteviews. settextviewtext (R. id. optional top_text, array_to_string (optional _text); intent clickintent = new intent (context, noteactivity. class); pendingintent pdintent = pendingintent. getactivity (context, 0, clickintent, 0); mremoteviews. setonclickpendingintent (R. id. widget_layout, pdintent); appwidgetmanager. updateappwidget (mappwidgetid, mremoteviews); }}// the onreceive method is used to receive broadcasts to update the desktop tag @ overridepublic void onreceive (context, intent) {// todo auto-generated method stubsuper. onreceive (context, intent); If (intent. getaction (). equals ("com. ideal. note. widget ") {dbhelper = new databasehelper (context," ideal. SQL "); sqlitedatabase DB = dbhelper. getreadabledatabase (); cursor = dB. query ("user", null, null); int comment _num = 0; Comment _text = new string [6]; while (cursor. movetonext () {If (1__num = 6) {break;} string temp_text = cursor. getstring (cursor. getcolumnindex ("mtext"); If (temp_text.length ()> 7) {temp_text = temp_text.substring (0, 7) + "... ";} pai_text [pai_num] = temp_text; system. out. println ("cmd_text [cmd_num]" + cmd_text [cmd_num]); cmd_num ++;} dB. close ();} remoteviews mremoteviews = new remoteviews (context. getpackagename (), R. layout. widget); mremoteviews. settextviewtext (R. id. optional top_text, array_to_string (desk_text); appwidgetmanager. getinstance (context ). updateappwidget (New componentname (context, appwidget. class), mremoteviews);} // The Public String array_to_string (string [] array) {string temp_str = ""; for (INT I = 0; I <array. length; I ++) {If (array [I] = NULL) {break;} else {temp_str = temp_str + "\ n *" + array [I];} return temp_str ;}}

I override onupdate and onreceive above. onupdate is executed when widgets are added to the desktop, and is also executed on Android: updateperiodmillis = "*****" (at least 30 minutes) run the command after the specified time. Onreceive is used to receive broadcasts sent after the database is modified to update desktop widgets.

As shown in onreceive above, when

if(intent.getAction().equals("com.ideal.note.widget"))

Then, read the database and update the desktop widget.

2. Send a broadcast source:

Notoactivity. Java is used as an example:

Intent mWidgetIntent = new Intent();mWidgetIntent.setAction("com.ideal.note.widget");NoteActivity.this.sendBroadcast(mWidgetIntent);

3. Note that you also need to register in androidmanifest. xml:

<receiver android:name=".AppWidget">             <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>                <action android:name="com.ideal.note.widget"/>            </intent-filter>            <meta-data  android:name="android.appwidget.provider"           android:resource="@xml/appwidget_info"/>         </receiver>

In this way, the desktop widget is updated as needed.

Ii. Use timer to update data:

Public class appwidget extends appwidgetprovider {private databasehelper dbhelper; string [] desk_text; handler mhandler; Context mcontext; @ overridepublic void onupdate (context, appwidgetmanager, int [] appwidgetids) {// todo auto-generated method stubsuper. onupdate (context, appwidgetmanager, appwidgetids); mcontext = context; timer mtimer = new timer (); mtimer. extends (New mytime (context, appwidgetmanager),);} private class mytime extends timertask {remoteviews mremoteviews; appwidgetmanager mappwidgetmanager; componentname thiswidget; Public mytime (context, appwidgetmanager) {This. mappwidgetmanager = appwidgetmanager; this. mremoteviews = new remoteviews (context. getpackagename (), R. layout. widget); thiswidget = new componentname (context, appwidget. class) ;}@ overridepublic void run () {// todo auto-generated method stubdbhelper = new databasehelper (mcontext, "ideal. SQL "); sqlitedatabase DB = dbhelper. getreadabledatabase (); cursor = dB. query ("user", null, null); int comment _num = 0; Comment _text = new string [6]; while (cursor. movetonext () {If (1__num = 6) {break;} string temp_text = cursor. getstring (cursor. getcolumnindex ("mtext"); If (temp_text.length ()> 7) {temp_text = temp_text.substring (0, 7) + "... ";} pai_text [pai_num] = temp_text; system. out. println ("cmd_text [cmd_num]" + cmd_text [cmd_num]); cmd_num ++;} dB. close (); mremoteviews. settextviewtext (R. id. optional top_text, array_to_string (optional _text); intent clickintent = new intent (mcontext, noteactivity. class); pendingintent pdintent = pendingintent. getactivity (mcontext, 0, clickintent, 0); mremoteviews. setonclickpendingintent (R. id. widget_layout, pdintent); mappwidgetmanager. updateappwidget (thiswidget, mremoteviews) ;}// the array is converted to the Public String array_to_string (string [] array) {string temp_str = ""; for (INT I = 0; I <array. length; I ++) {If (array [I] = NULL) {break;} else {temp_str = temp_str + "\ n *" + array [I];} return temp_str ;}}

This method updates the desktop widget every second, but it consumes too much resources and makes the desktop very stuck. It should be better to use threads for updates.

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.