Android-receives broadcasts from appwidgets and updates the status of Appwidget controls
I heard that desktop users have to fill it out. For PC users, 360 and QQ are basically the most frequently used applications. Each program has corresponding controls in the lower right corner of the PC Desktop, the longer it takes to occupy the desktop, the more users use it. This will bring more benefits to applications. The same is true for mobile phone desktops. Naturally, most of them are desktops. Then, how can I add my own controls to the desktop and change the control status.
I made a simple example myself: when the image button is clicked, the image below will be updated to another image.
Create a project: AppWidget03
Project running effect:
Steps:
1. Define the layout file: appwidget_provider_layout.xml
2. Create the xml directory under the res directory and create the xml file: appwidget_provider.xml
3. Register aggreger under the manifest File
4. New Appwidget class inherits AppwidgetProvider
5. Override the OnUpdate method of AppwidgetProvider and the OnReceiver method.
[Html]
<? 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">
<ImageButton
Android: id = "@ + id/imageButton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/th_desktop"/>
<ImageView
Android: id = "@ + id/imageView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/th_twitter"
/>
</LinearLayout>
<? 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">
<ImageButton
Android: id = "@ + id/imageButton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/th_desktop"/>
<ImageView
Android: id = "@ + id/imageView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/th_twitter"
/>
</LinearLayout>
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Appwidget-provider xmlns: android = "http://schemas.android.com/apk/res/android"
Android: minWidth = "292dp"
Android: minHeight = "72dp"
Android: updatePeriodMillis = "30000"
Android: initialLayout = "@ layout/main"
>
</Appwidget-provider>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Appwidget-provider xmlns: android = "http://schemas.android.com/apk/res/android"
Android: minWidth = "292dp"
Android: minHeight = "72dp"
Android: updatePeriodMillis = "30000"
Android: initialLayout = "@ layout/main"
>
</Appwidget-provider>
[Html]
<Cycler android: name = "AppWidget">
<Intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"/>
</Intent-filter>
<Intent-filter>
<Action android: name = "org. wwj. appwidget. UPDATE_APP_WIDGET"/>
</Intent-filter>
<Meta-data android: name = "android. appwidget. provider"
Android: resource = "@ xml/appwidget_provider"/>
</Cycler>
<Cycler android: name = "AppWidget">
<Intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"/>
</Intent-filter>
<Intent-filter>
<Action android: name = "org. wwj. appwidget. UPDATE_APP_WIDGET"/>
</Intent-filter>
<Meta-data android: name = "android. appwidget. provider"
Android: resource = "@ xml/appwidget_provider"/>
</Cycler>
[Java]
Package mars. appwidget03;
Import android. app. PendingIntent;
Import android. appwidget. AppWidgetManager;
Import android. appwidget. AppWidgetProvider;
Import android. content. Context;
Import android. content. Intent;
Import android. widget. RemoteViews;
Public class AppWidget extends AppWidgetProvider {
// Define a constant string used to name the Action
Private static final String UPDATE_ACTION = "mars. appwidget03.UPDATE _ APP_WIDGET ";
@ 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 ){
Super. onReceive (context, intent );
String action = intent. getAction ();
If (UPDATE_ACTION.equals (action )){
System. out. println ("onReceive --->" + UPDATE_ACTION );
}
}
@ Override
Public void onUpdate (Context context, AppWidgetManager appWidgetManager,
Int [] appWidgetIds ){
// Create an Intent object
Intent intent = new Intent ();
// Set Action for the Intent object
Intent. setAction (UPDATE_ACTION );
// Use the getBroadcast method to obtain a PendingIntent object. When this object is executed, a broadcast is sent.
PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0,
Intent, 0 );
RemoteViews remoteViews = new RemoteViews (context. getPackageName (),
R. layout. main );
RemoteViews. setOnClickPendingIntent (R. id. imageButton, pendingIntent );
AppWidgetManager. updateAppWidget (appWidgetIds, remoteViews );
}
}
Package mars. appwidget03;
Import android. app. PendingIntent;
Import android. appwidget. AppWidgetManager;
Import android. appwidget. AppWidgetProvider;
Import android. content. Context;
Import android. content. Intent;
Import android. widget. RemoteViews;
Public class AppWidget extends AppWidgetProvider {
// Define a constant string used to name the Action
Private static final String UPDATE_ACTION = "mars. appwidget03.UPDATE _ APP_WIDGET ";
@ 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 ){
Super. onReceive (context, intent );
String action = intent. getAction ();
If (UPDATE_ACTION.equals (action )){
System. out. println ("onReceive --->" + UPDATE_ACTION );
}
}
@ Override
Public void onUpdate (Context context, AppWidgetManager appWidgetManager,
Int [] appWidgetIds ){
// Create an Intent object
Intent intent = new Intent ();
// Set Action for the Intent object
Intent. setAction (UPDATE_ACTION );
// Use the getBroadcast method to obtain a PendingIntent object. When this object is executed, a broadcast is sent.
PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0,
Intent, 0 );
RemoteViews remoteViews = new RemoteViews (context. getPackageName (),
R. layout. main );
RemoteViews. setOnClickPendingIntent (R. id. imageButton, pendingIntent );
AppWidgetManager. updateAppWidget (appWidgetIds, remoteViews );
}
}
Author: wwj_748