Life Cycle example Analysis of widget in Android development _android

Source: Internet
Author: User
Tags stub

This article analyzes the life cycle of widgets in Android development. Share to everyone for your reference, specific as follows:

Widgets are Android desktop widgets that must be inherited when created Appwidgetprovider,appwidgetprovider is actually inherited from the receiver of Broadcastreceiver, The widget has several lifecycle methods:

1.onenabled Method : This method is called at the first time the widget is created, and is called only once, and is often placed in the initialization data, the operation of the service.

2.OnReceive method : Broadcastreceiver OnReceive method, but here is the difference is that when the widget operation is received the first call is the OnReceive method, and then the relevant method of operation. It is also well understood that widgets are small controls that run on the desktop application, and when their applications need to invoke the widget, they need to send a broadcast event to invoke it.

3.OnUpdate: The method that the widget calls when it updates at a fixed time.

4.ondeleted: Method to invoke when widget is deleted.

5.ondisabled: The used widget is removed as a method of invocation, as opposed to the Onenabled method.

Examples and notes:

Package cn.itcast.testwidget;
Import Android.appwidget.AppWidgetManager;
Import Android.appwidget.AppWidgetProvider;
Import Android.content.Context;
Import android.content.Intent; /** * Update the interface according to the profile at regular intervals * Minimum value half an hour 1800000 milliseconds * Onrecevie-> OnUpdate * * * Note widget This component is not realistic in our application * Show Applications on the desktop * different desktops for their widget creation and destruction the corresponding callback events may have an inability to * Android LUNCHER/HTC sence/M ui/360 desktop/AWT/QQ Desktop/... * * * */PUB Lic class Mywidget extends Appwidgetprovider {@Override public void onreceive (context context, Intent Intent) {/
    /TODO auto-generated Method Stub super.onreceive (context, intent);
  System.out.println ("OnReceive"); @Override public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {S
    Ystem.out.println ("OnUpdate");
  Super.onupdate (context, Appwidgetmanager, appwidgetids); @Override public void ondeleted (context context, int[] appwidgetids) {//TODO auto-generated method stub Sy Stem. OUT.PRINTLN ("ondeleted");
    Super.ondeleted (context, appwidgetids); When a widget is deleted, the OnDelete method is executed @Override public void onenabled {//TODO auto-generated met
    Hod stub System.out.println ("onenabled");
  Widget first created by the method//initialization widget data operation, after opening the background super.onenabled (context); @Override public void ondisabled (context context) {//TODO auto-generated Method stub super.ondisabled (cont
    EXT);
    System.out.println ("ondisabled"); Execute ondisable () when all widgets are deleted ()//stop our open Services/delete junk files temporary files}}

More interested readers of Android-related content can view this site: "Introduction to Android Development and advanced Course", "Android Communication Summary", "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls

I hope this article will help you with the Android program.

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.