Android app widget (3)

Source: Internet
Author: User

1. receive broadcasts from app Widgets

// The purpose of the file in androidmanifest. As long as there is an action that matches, the class inheriting appwidgetprovider will be called:

<Cycler Android: Name = "inherit the appwidgetprovider class">

<Intent-filter>

<Action Android: Name = "android. appwidget. Action. appwidget_updata"/>

</Intent-filter>

<Intent-filter>

<Action Android: Name = "Mars. appwidget03.update _ app_widget"/> // This is a custom action

</Intent-filter>

<Meta-data Android: Name = "android. appwidget. provider"

Android: Resource = "@ XML/example_appwidget_info"/>

</Cycler>

// The process of inheriting the appwidgetprovider class in the Code is as follows:

     A.In the update method

Intent intent = new intent ();

Intent. setaction ("Mars. appwidget03.update _ app_widget ");

Use the getbroadcast method to obtain the pendingintent object

Pendingintent = pendingintent. getbroadcast (contect, 0, intent object, 0 );

B.Then, the onreciever method receives the broadcast sent by step.

    

2. Update the widget status in the app widget.

Public void onreceive (context, intent ){

String action = intent. getaction ();

If (Action = "Mars. appwidget03.update _ app_widget "){

Remoteviews = new remoteviews (context. getpackage, R. layout. example_appwidget );

// Modify the image

Remoteviews. setimageviewresource (R. Id. imageid, R. drawable. Ku );

Remoteviews. Settextviewtext (R. Id. widgettextid ,"");

// Obtain the appwidgetmanager object

Appwidgetmanager = appwidgetmanager. getinstance (context );

// Componentname refers to the entire app widget control, while remoteviews refers to the controls in the app widget.

Componentname = new componentname (context, exampleappwidgetprovider. Class );

// Update the appwidget

Appwidgetmanager. updateappwidget (componentname, remoteviews );

}

// Else statement is required. Otherwise, other functions such as onupdate and onenable will not be called.

Else {

Super onreceive (context, intent );

}

}

 

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.