Android desktop controls:
The view in appwidget runs in the home screen process, which is not in the same process as our application.
Appwidgetproviderinfo object:
Provides metadata for app widgets, including layout and update frequency. This object is defined in an XML file.
Appwidgetprovider: defines the basic lifecycle function of appwidget.
Method: onupdate: after the specified Update time is reached, the latter will be called when the user adds an app widget to the desktop.
This method.
Ondeleted: This method is called when the app widget is deleted.
Onenabled: called when an app widget instance is created for the first time
Ondisabled: This method is called when the last app widget instance is deleted.
Onreveice: receives broadcast events
Steps:
1. Create the directory XML under the res directory and create the file widget_info.xml.
<? XML version = "1.0" encoding = "UTF-8"?>
<Appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: minwidth = "294dp"
Android: minheight = "72dp"
Android: updateperiodmillis = "86400000"
Android: initiallayout = "@ layout/appwidget">
</Appwidget-provider>
2. Create the layout file appwidget. XML in layout:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Textview
Android: Id = "@ + ID/widgettextld"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "firstwidgettext"
Android: Background = "#000000"
> </Textview>
</Linearlayout>
3. Add the receiver tag to the androidmanifest. xml file:
<Cycler Android: Name = "appwidgetprovidertest">
<Intent-filter>
<Action
Android: Name = "android. appwidget. Action. appwidget_update"/>
</Intent-filter>
<Meta-data Android: Name = "android. appwidget. provider"
Android: Resource = "@ XML/appwidget_info"
> </Meta-data>
</Cycler>
4. New Class: appwidgetprovidertest. Java, inherited from the appwidgetprovider implementation method.
Package com. gufengxiachen. Android;
Import Android. appwidget. appwidgetmanager;
Import Android. appwidget. appwidgetprovider;
Import Android. content. context;
Import Android. content. intent;
Public class appwidgetprovidertest extends appwidgetprovider {
@ Override
Public void ondeleted (context, int [] appwidgetids ){
// Todo auto-generated method stub
System. Out. println ("deleted ");
Super. ondeleted (context, appwidgetids );
}
@ Override
Public void onenabled (context ){
// Todo auto-generated method stub
System. Out. println ("enabled ");
Super. onenabled (context );
}
@ Override
Public void ondisabled (context ){
// Todo auto-generated method stub
System. Out. println ("disabled ");
Super. ondisabled (context );
}
@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
System. Out. println ("receive ");
Super. onreceive (context, intent );
}
@ Override
Public void onupdate (context, appwidgetmanager
Appwidgetmanager,
Int [] appwidgetids ){
// Todo auto-generated method stub
System. Out. println ("Update ");
Super. onupdate (context, appwidgetmanager, appwidgetids );
}
}
Pendingintent: encapsulates intent that is not used for the time being and intent that is delayed for execution. Mainly similar to the specific android
Process Service (SMS, notification, or alert), which is usually not used by applications.
Construct a pendingintent object:
Static Method:
Getactivity (content, int requestcode, intent, int flags );
Getbroadcast (content, int requestcode, intent, int flags );
Getservice (content, int requestcode, intent, int flags );
Remoteviews: represents a series of view objects. The remoteviews running object is in another process.
Control listener bound to appwidget:
Remoteviews. setonclickpendingintent (R. Id. widgetbuttonld, pendingintent );
Main Code:
For (INT I = 0; I <appwidgetids. length; I ++ ){
System. Out. println (appwidgetids [I]);
// Define the intent object
Intent intent = new intent
(Context, targetactivity. Class );
// Define the pendingintent object and wrap the intent object
Pendingintent =
Pendingintent. getactivity (context, 0, intent, 0 );
// Obtain the remoteviews object
Remoteviews = new remoteviews
(Context. getpackagename (), R. layout. appwidget );
// Add a listener to the control
Remoteviews. setonclickpendingintent
(R. Id. widgetbuttonld, pendingintent );
// Update the app widget
Appwidgetmanager. updateappwidget (appwidgetids [I],
Remoteviews );
}
Super. onupdate (context, appwidgetmanager, appwidgetids );
}
Receive broadcasts from appwidget:
1. register a new intent-filter for appwidgetprovider in androidmanifest. xml;
2. Use the getbroadcast () method to create a pendingintent;
3. register the processor for the controls in the appwidget
4. Receive broadcast information in the onreceive () method.