Research on Android Widgets

Source: Internet
Author: User

For more information about the previous configurations such as mainfest. XML, XML/widget, Res/layout/my_widget, see the API documentation.

 

 

I. Configure widgets.

 

You need to pay attention to the following points for setting the pluate of widgets:

Intent intent = new Intent();
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
new int[] { mAppWidgetId });
context.sendBroadcast(intent);
setResult(RESULT_OK, intent);

1. send the current intent through sendbroadcast. The action must be appwidgetmanager. action_appwidget_update; in this way, the widgetprovider you write can be connected, because widgetprovider itself is reciver, and action is appwidgetmanager. action_appwidget_update to implement call widgetprovider

 

2.Be sure to write setresult (result_ OK, intent); otherwise the widget will not be placed on the desktopGo (I forgot to test it for a long time. Very depressing)

 

 

 

E/androidruntime (1785): caused by: Java. Lang. nullpointerexception

E/androidruntime (1785): At com. Android. launcher. launcher. completeaddappwidget (launcher. Java: 657)

E/androidruntime (1785): At com. Android. launcher. launcher. onactivityresult (launcher. Java: 352)

E/androidruntime (1785): at Android. App. activity. dispatchactivityresult (activity. Java: 3595)

E/androidruntime (1785): at Android. App. activitythread. deliverresults (activitythread. Java: 3001)

 

Because the corresponding widget ID is added to workspace. Java in the corresponding laucher.

 

 

II. Write widgetprovidrer

 

1. Write widgetprovider

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
defaultAppWidget(context, appWidgetIds);
context.startService(updateIntent);

}

The processing of defaappappwidget is mainly to get the current remoteviews and then set the listener for the buttons and so on.

Pendingintent is generally used for listener settings. Because remoteviews only supports setonclickpendingintent (ID, pendingintent );

The pendintent can be obtained in two ways. One is pendingintent. getactivity (); that is, jump to the corresponding activity to process your affair.

 

 

Pendingintent = pendingintent. getactivity (context,

0/* No requestcode */, intent, 0/* no flags */);

Views. setonclickpendingintent (R. Id. ivcover, pendingintent );

 

The second is to call servers for processing.

 

Pendingintent = pendingintent. getservice (context,

0/* No requestcode */, intent, 0/* no flags */);

Views. setonclickpendingintent (R. Id. btnnext, pendingintent );

 

 

 

3. Write Server

 

Oncreate is generally used to instantiate something during the first call.

Generally, the action you passed is processed in onstart ().

 

After processing the corresponding servers, call the code for updating the UI in widgetprovider.

 

 

References:

Music Widgets

Search Widgets

If you are interested, you can view the source code directly!

 

 

With the popularity of Android 1.5, many users find that the appwidget framework is added to the SDK. Widgets designed using the Java language have better running efficiency and lower-layer control capabilities than the mainstream W3C widgets. Today we will mainly explain the development of widgets on the Android platform.

First, we need to add a receiver node to the androidmanifest. xml file to receive System Broadcasts. To declare the broadcastreceiver class, we can directly use intent filter to filter system information. The following is the method from the SDK.

<Cycler Android: Name = "testappwidgetprovider" Android: Label = "@ string/cwj" Android: icon = "@ drawable/smart_icon">
<Intent-filter>
<Action Android: Name = "android. appwidget. Action. appwidget_update"/> // here we need to capture the appwidget_update action.
</Intent-filter>
<Meta-data Android: Name = "android. appwidget. provider" Android: Resource = "@ XML/appwidget_info"/> // define meta data. For details, see appwidget-provider
</Cycler>

We define an appwidgetproviderinfo structure to describe the appearance of this widget. Here it is similar to the layout. You can set the minimum width to 40dp and the minimum height to 30dp.

<Appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android" Android: minwidth =" 40dp"
Android: minheight = "30dp"
Android: updateperiodmillis = "86400000" // callback timer, in milliseconds. Here is 1 day = 60x60x24x1000. The power of the Android mobile phone is limited and the frequency cannot be too high. It is best for users to control it by themselves.
Android: initiallayout = "@ layout/test_appwidget" // main layout during initialization
Android: Configure = "com. Android. Tests. appwidgethost. testappwidgetconfigure">
</Appwidget-provider>

In widgets, appwidgetprovider is used as the base class. The broadcastreceiver we just defined is mainly used to update the action_appwidget_update action, in addition, there are also action_appwidget_deleted, action_appwidget_enabled, and action_appwidget_disabled, which indicate the events triggered when the deletion is enabled, and disabled. The system broadcast is used here. We use the onreceive method of the broadcastreceiver class to capture.

The following Java broadcast action acquisition code is as follows:

Public void onreceive (context, intent ){
String action = intent. getaction (); // get the current action
If (appwidgetmanager. action_appwidget_update.equals (Action) {// if it is an action_appwidget_update action
Bundle extras = intent. getextras ();
If (extras! = NULL ){
Int [] appwidgetids = extras. getintarray (appwidgetmanager. extra_appwidget_ids );
If (appwidgetids! = NULL & appwidgetids. length> 0 ){
This. onupdate (context, appwidgetmanager. getinstance (context), appwidgetids );
}
}
}
Else if (appwidgetmanager. action_appwidget_deleted.equals (Action) {// action 2
Bundle extras = intent. getextras ();
If (extras! = NULL ){
Int [] appwidgetids = extras. getintarray (appwidgetmanager. extra_appwidget_ids );
If (appwidgetids! = NULL & appwidgetids. length> 0 ){
This. ondeleted (context, appwidgetids );
}
}
}
Else if (appwidgetmanager. action_appwidget_enabled.equals (Action) {// Action 3
This. onenabled (context );
}
Else if (appwidgetmanager. action_appwidget_disabled.equals (Action) {// Action 4
This. ondisabled (context );
}
}

In this article, we will briefly describe the widget framework of the Android platform. In the future, we will explain it through actual examples, we can see that the widgets here are different from the common W3C tools such as dashboard on the iPhone that use HTML + Js in Ajax mode, but the overall development is simpler than the android standard program, we mainly consider the UI painting and network communication. We recommend that you take a look at Apache communication.

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.