AppWidget for android Learning
1. What is AppWidget
The small windows seen on the desktop can provide users with convenient and quick operations, a little like a shortcut...
Ii. AppWidget-related data
1. The AppWidgetProviderInfo object provides metadata for the appWidget, including layout and update frequency. This object is defined in the xml file.
2. appWidgetProvider defines the basic lifecycle of appWidget
3. Create an AppWidget
1. Define the AppWidgetProviderInfo object
Define a file named example_appwidget_info.xml In the res/xml folder.
<Appwidget-provider
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: minWidth = "294dp" <? Minimum Width?>
Android: minHeight = "72dp" <? Minimum height?>
Android: updatePeriodMillis = "86400000" <? Update time, in ms?>
Android: initialLayout = "@ layout/example_appwidget" <? The layout file initialized by appwidget?>
/>
2. Add a layout file for the AppWidget
<TextView
Android: id = "@ + id/widgetText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "myWidget"
/>
3. Implement appWidgetProvider
OnUpdate: it is called after the specified Update Time on the avenue or when you want to add an appwidget to the desktop.
OnDelete: called when the appWidget is deleted
OnEnabled: called when appwidget is created for the first time
OnDisabled: called when the last appwidget is deleted
OnRecieve: The appwidget that receives broadcasts relies on the broadcast mechanism.
4. Declare broadcast in AndroidManifest
<Cycler android: name = "com. example. appwidget. appWidget">
<Intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"/>
</Intent-filter>
<Meta-data
Android: name = "android. appwidget. provider"
Android: resource = "@ xml/example_appwidget_info"
/>
</Cycler>
Iv. How to Use AppWidget
1. pendingIntent is used to exchange data between appwidget and other activities. The appwidget and the activity that creates it are not in the same process.
2. How to Create pendingIntent:
(1) getActivity () // used to start a new activity
(2) getBroadcast () // used to start a new broadcast. The broadcast will receive it in the onReceive method.
(3), getService () // used to start a new service
3. remoteViews indicates a series of view objects in another thread.
4. Method for adding a listener to remoteViews: setOnClickPendingIntent
V. Source Code
1. MainActivity. java
No operation is required here.
- public class MainActivity extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
2. myAppWidget. java
Operate AppWidget here
- Public class appWidget extends AppWidgetProvider
- {
- // Define a constant for Custom action
- Private static final String UPDATE_ACTION = "appwidget. UPDATE_APPWIDGET ";
- @ Override
- Public void onDeleted (Context context, int [] appWidgetIds)
- {
- // TODO Auto-generated method stub
- Super. onDeleted (context, appWidgetIds );
- }
- @ Override
- Public void onDisabled (Context context)
- {
- // TODO Auto-generated method stub
- Super. onDisabled (context );
- }
- @ Override
- Public void onEnabled (Context context)
- {
- // TODO Auto-generated method stub
- Super. onEnabled (context );
- }
- @ Override
- Public void onReceive (Context context, Intent intent)
- {
- // TODO Auto-generated method stub
- String action = intent. getAction ();
- If (UPDATE_ACTION.equals (action ))
- {
- System. out. println (action );
- // Obtain remoteViews
- RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. example_appwidget );
- // Set the action for remoteViews
- RemoteViews. setTextViewText (R. id. widgetText, "hello ");
- // Create AppWidgetManager
- AppWidgetManager appWidgetManager = AppWidgetManager. getInstance (context );
- // Create ComponentName
- ComponentName compenentName = new ComponentName (context, appWidget. class );
- AppWidgetManager. updateAppWidget (compenentName, remoteViews );
- }
- Else
- {
- Super. onReceive (context, intent );
- }
- }
- @ Override
- Public void onUpdate (Context context, AppWidgetManager appWidgetManager,
- Int [] appWidgetIds)
- {
- // Each time an appwidget is created, an id is added.
- For (int I = 0; I <appWidgetIds. length; I ++)
- {
- System. out. println (appWidgetIds [I]);
- // Create an intent object
- Intent intent = new Intent (context, targetActivity. class );
- // Create a pendingIntent object and use getActivity to start a new activity.
- PendingIntent pendingIntent = PendingIntent. getActivity (context, 0, intent, 0 );
- // Create remoteViews
- RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. example_appwidget );
- // Bind the event processor to remoteViews and execute pendingIntent after a click event occurs.
- // The first parameter is the bound control, and the second parameter specifies the pendingIntent to be executed when an event occurs.
- RemoteViews. setOnClickPendingIntent (R. id. appWidgetBtn1, pendingIntent );
- // Update the appwidget
- AppWidgetManager. updateAppWidget (appWidgetIds [I], remoteViews );
- }
- For (int I = 0; I <appWidgetIds. length; I ++)
- {
- System. out. println (appWidgetIds [I]);
- // Create an intent object
- Intent intent = new Intent ();
- // Set the action, which must be declared in androidManifest.
- Intent. setAction (UPDATE_ACTION );
- // Create a pendingIntent object and use getBroadcast to send broadcasts. The sent broadcasts are received in the onReceive method.
- PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0, intent, 0 );
- // Create remoteViews
- RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. example_appwidget );
- // Bind the event processor to remoteViews and execute pendingIntent after a click event occurs.
- // The first parameter is the bound control, and the second parameter specifies the pendingIntent to be executed when an event occurs.
- RemoteViews. setOnClickPendingIntent (R. id. appWidgetBtn2, pendingIntent );
- // Update the appwidget
- AppWidgetManager. updateAppWidget (appWidgetIds [I], remoteViews );
- }
- // TODO Auto-generated method stub
- Super. onUpdate (context, appWidgetManager, appWidgetIds );
- }
- }
4. example_appwidget_info.xml
This xml file is not a layout file, it provides metadata for the AppWidget
- <?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/example_appwidget"
- />
5. example_appwiet.xml
This is the layout file of the AppWidget.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/widgetText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="myWidget"
- android:background="#000000"
- />
- <Button
- android:id = "@+id/appWidgetBtn1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="test Button1"
- />
- <Button
- android:id = "@+id/appWidgetBtn2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="test Button2"
- />
- </LinearLayout>
6. activity_main.xml
This is the layout file of MainActivity.
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- </RelativeLayout>