Android Desktop component [App widget] advanced project-Mood Recorder

Source: Internet
Author: User

Http://www.cnblogs.com/TerryBlog/archive/2010/08/10/1796896.html

In the previous article about Android app widgets, we continued to expand the previous example and completed a small project, which can be used directly. Address of the previous Article: A Preliminary Study of the android Desktop component [widget]. If you have never touched the app widget, you can take a look at it to help you understand the code in this article.

Project name completed in this article: [Mood Recorder]

You can use desktop components to display your mood information on the desktop, and have a wealth of emojis to choose from and load on the desktop. This function is similar to the various signatures on QQ, although the mobile phone is our own, we use an Android phone. It is inevitable that some friends will bring it to play. At this time, we can see that the mobile phone owner's mood is not very good? Maybe you can secretly record some unhappy ideas with your mood. Note: This function does not support saving multiple moods. You can only save one mood. If you need it, you can give me some suggestions later, of course, I think I do not want this function to save my feelings .. Leave a message if you want.

Well, it's a lot of nonsense. Let's take a look at it first:

Tip: this little item is completely an app widget Desktop component, so you must press the desktop or click menu to call it out.

 

The demo of the previous app widget only adds a click event for textview. This article will change to another method. Click layout to bring up an activity operation interface. Then, you can select an expression and save your mood on this operation interface. How can you click to open an activity interface?

  • Method 1:
    Register a broadcast in updateappwidget of our component, add a click broadcast to textview, and then run the following code in onreceive receiving broadcast: intent INTN = new intent (context, update. Class );
    INTN. setflags (intent. flag_activity_new_task );
    Context. startactivity (INTN );

     

    By re-setting a new task for intent to open the activity, this method can start an activity. Of course, this method is not recommended because an action is repeated, repeat the details. For more information about registering a broadcast for textview, see the link provided above.

  • Method 2:
    This method is intended to tell you how to repeat it, that is, if we can register a broadcast for it, then why do we do broadcast click jump for it without simply doing it? The reference code is as follows: intent intentclick = new intent (context, update. Class );
    Pendingintent = pendingintent. getactivity (context, 0,
    Intentclick, 0 );
    RV. setonclickpendingintent (R. Id. layout, pendingintent );

     

    Through the settings here, you can achieve the desired effect without receiving broadcasts.

The following is an example of opening the activity operation interface. I am a little embarrassed here, because the impromptu painting is ugly, so we will take a look at it, mainly to understand how APP widgets are written.

How to dynamically interact with the app widget by clicking save? Let's take a look at the following code.

 

Remoteviews views = new remoteviews (update. This
. Getpackagename (), R. layout. Main );
Views. settextviewtext (R. Id. textview01, text );
Views. setimageviewresource (R. Id. imageview01, util. Image [Index]);
Componentname widget = new componentname (update. This,
Widgetprovider. Class );
Appwidgetmanager manager = appwidgetmanager
. Getinstance (update. This );
Manager. updateappwidget (widget, views );

 

 

Remoteviews is also used here to receive value changes, and then change remoteviews through the Desktop component manager appwidgetmanager. Because we only use key-value pairs to save the user record data at the moment.

The following code is provided:

 

Package com. Terry;

Import Android. App. activity;
Import Android. appwidget. appwidgetmanager;
Import Android. content. componentname;
Import Android. content. sharedpreferences;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. adapterview;
Import Android. widget. arrayadapter;
Import Android. widget. Button;
Import Android. widget. edittext;
Import Android. widget. imageview;
Import Android. widget. remoteviews;
Import Android. widget. spinner;
Import Android. widget. adapterview. onitemselectedlistener;

Public class update extends activity {

Private edittext medittext;
Private button mbutton;

Private spinner mspinner;

Private int Index = 0;

@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Update );

Medittext = (edittext) findviewbyid (R. Id. edittext01 );
Mbutton = (button) findviewbyid (R. Id. button01 );
Mspinner = (spinner) findviewbyid (R. Id. spinner01 );
Final imageview IV = (imageview) findviewbyid (R. Id. imageview01 );
Arrayadapter <string> adpter = new arrayadapter <string> (this,
Android. R. layout. simple_spinner_dropdown_item, util. Text );
Adpter
. Setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
Mspinner. setadapter (adpter );

Sharedpreferences settings = getsharedpreferences ("settinginfo ",
Activity. mode_private );
Index = settings. getint ("imagestate", 0 );
Medittext. settext (settings. getstring ("heart ",""));
Iv. setimageresource (util. Image [Index]);
Mspinner. setselection (INDEX );
Mspinner. setonitemselectedlistener (New onitemselectedlistener (){

@ Override
Public void onitemselected (adapterview <?> Arg0, view arg1,
Int arg2, long arg3 ){
// Todo auto-generated method stub
Index = arg2;

Iv. setimageresource (util. Image [Index]);

}

@ Override
Public void onnothingselected (adapterview <?> Arg0 ){
// Todo auto-generated method stub

}
});
Mbutton. setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
String text = medittext. gettext (). tostring ();
If (text. Equals ("")){
Return;
}
Sharedpreferences shared = getsharedpreferences ("settinginfo ",
Activity. mode_private );
Sharedpreferences. Editor editor = shared. Edit ();
Editor. putint ("imagestate", index );
Editor. putstring ("heart", text );
Editor. Commit ();
Remoteviews views = new remoteviews (update. This
. Getpackagename (), R. layout. Main );
Views. settextviewtext (R. Id. textview01, text );
Views. setimageviewresource (R. Id. imageview01, util. Image [Index]);
Componentname widget = new componentname (update. This,
Widgetprovider. Class );
Appwidgetmanager manager = appwidgetmanager
. Getinstance (update. This );
Manager. updateappwidget (widget, views );
Update. This. Finish ();

}
});

}
}

 

The updateappwidget method is called every time a component is created. Therefore, the method must also obtain the key-value pair.

 

Package com. Terry;

Import Android. App. activity;
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. content. sharedpreferences;
Import Android. widget. remoteviews;
Import Android. widget. Toast;

Public class widgetprovider extends appwidgetprovider {

Private Static remoteviews RV;

@ Override
Public void onupdate (context, appwidgetmanager,
Int [] appwidgetids ){
// Todo auto-generated method stub
Final int n = appwidgetids. length;
For (INT I = 0; I <n; I ++ ){
Int appwidgetid = appwidgetids [I];
Updateappwidget (context, appwidgetmanager, appwidgetid );
}
}

@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
Super. onreceive (context, intent );



}

Public static void updateappwidget (context,
Appwidgetmanager appwidgemanger, int appwidgetid ){
Rv = new remoteviews (context. getpackagename (), R. layout. Main );

Sharedpreferences shared = context. getsharedpreferences ("settinginfo ",
Activity. mode_private );
// Util. Index = settings. getint ("imagestate", 0 );
// Medittext. settext (settings. getstring ("heart ",""));
RV. settextviewtext (R. Id. textview01, shared. getstring ("heart", Context
. Getresources (). getstring (R. String. Load )));
RV. setimageviewresource (R. Id. imageview01, util. Image [shared. getint (
"Imagestate", 0)]);

Intent intentclick = new intent (context, update. Class );
Pendingintent = pendingintent. getactivity (context, 0,
Intentclick, 0 );
RV. setonclickpendingintent (R. Id. layout, pendingintent );
Appwidgemanger. updateappwidget (appwidgetid, RV );
}
}

 

 

 

Tip: all the knowledge points mentioned above are mentioned in all previous articles. If you do not understand them, you can make your own reference.

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.