Android AppWidgetProvider-based usage

Source: Internet
Author: User

AppWidgetProvider is used to display plug-ins on the HOME page.

Steps:
1. Provide AppWidget with an meta-layout file appwideprovider_provider.xml to display the Widget interface.
2. Create a class that inherits from AppWidgetProvider and overwrite the related methods in it.
3. Create a referenced layout file for WidgetProvider or directly use main. xml.
4. Register Manifest. xml in the program.

The Code is as follows:

1. Create the appwideprovider_provider.xml file in the res/xml/folder.

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Appwidget-provider xmlns: android = "http://schemas.android.com/apk/res/android"
Android: initialLayout = "@ layout/main" <! -- Android: initialLayout: sets the referenced layout file -->
Android: minHeight = "50dip"
Android: minWidth = "50dip"
Android: updatePeriodMillis = "5000" type = "codeph" text = "codeph"> <! -- Set the update time, in milliseconds -->

</Appwidget-provider>

2. Modify MainActivity to inherit from AppWidgetProvider and override some methods in itCopy codeThe Code is as follows: public class MainActivity extends AppWidgetProvider {

@ Override
Public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds ){
Super. onUpdate (context, appWidgetManager, appWidgetIds );
Timer timer = new Timer ();
Timer. scheduleAtFixedRate (new LYTimeTask (context, appWidgetManager), 1, 50000 );
}

Private class LYTimeTask extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName widget;

@ Override
Public void run (){
Date date = new Date ();
Calendar calendar = new GregorianCalendar (2013, 07, 24 );
Long days = (calendar. getTimeInMillis ()-date. getTime ()/1000/86400;
RemoteViews. setTextViewText (R. id. worldcup, "from baby birthday" + days + "days ");
AppWidgetManager. updateAppWidget (widget, remoteViews );
}

Public LYTimeTask (Context context, AppWidgetManager appWidgetManger ){
Super ();
This. appWidgetManager = appWidgetManger;
RemoteViews = new RemoteViews (context. getPackageName (), R. layout. activity_main );
Widget = new ComponentName (context, MainActivity. class );
}
};
}

3. Create a layout file for display for the Widget: main. xml

Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "@ drawable/worldcup"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/babybirthday"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/message"
Android: textSize = "12px"
Android: textColor = "# ff0000"/>
</LinearLayout>

4. Modify the inventory file automatically generated by the program. In AndroidManifest. xml, declare that the sub-class of the above AppWidgetProvider is a aggreger and:

(1) the intent-filter Action of the attacker must contain "android. appwidget. action. APPWIDGET_UPDATE ";

(2) The metadata-data of the handler is "android. appwidget. provider" and describes the layout attributes in an xml file.

Copy codeThe Code is as follows: <application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Cycler
Android: name = ". MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"/> <! -- Broadcast receiving filter -->
</Intent-filter>

<Meta-data
Android: name = "android. appwidget. provider"
Android: resource = "@ xml/appwiggdetprovider_provider"/> <! -- The Provider file referenced by AppWidgetProvider -->
</Cycler>
</Application>

Run the program: Enter the WIDGETS page to add WIDGETS to the HOME page.

There are other related methods in the AppWidgetProvider class.

Copy codeThe Code is as follows:
Public class WidgetProvider extends AppWidgetProvider {

// Call each time a broadcast message is received, which is frequently used.
Public void onReceive (Context context, Intent intent ){
Super. onReceive (context, intent );
}

// This method is called once for each update and is frequently used.
Public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds ){
Super. onUpdate (context, appWidgetManager, appWidgetIds );
}

// Call each time you delete one
Public void onDeleted (Context context, int [] appWidgetIds ){
Super. onDeleted (context, appWidgetIds );
}

// This method is called when the Widget is added to the desktop for the first time. It can be added multiple times but only called for the first time.
Public void onEnabled (Context context ){
Super. onEnabled (context );
}

// When the last Widget is deleted, this method is called. Note that the last Widget is deleted.
Public void onDisabled (Context context ){
Super. onDisabled (context );
}
}

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.