[Android] develop a desktop plug-in (3)

Source: Internet
Author: User

Reprinted from: http://www.eoeandroid.com/thread-71213-1-1.html


This is the last article of our small project, which is expected by everyone. Let's take a look at the last article.



Note: Because this appwidgetprovider is a broadcast receiver broadcastreceiver, it is not guaranteed that your process continues to run after the callback function returns (see application basics> broadcast receiver lifecycle Application Fundamentals> broadcast receiver lifecycle for more information ). If your app widget setting process lasts for several seconds (maybe when a webpage request is executed) And you want your process to continue, consider starting a service in the onupdated () method.
. From this service, you can execute your own app widget update, without worrying that the appwidgetprovider is disabled because an application does not respond to the application not responding (ANR) error. See wiktionary sample's appwidgetprovider. This is an example of an app widget running a service.


For more information, see exampleappwidgetprovider. java.


Receive app widget broadcast intent
Appwidgetprovider is just a simple class. If you want to directly receive app widget broadcasts, You can implement your own broadcastreceiver or rewrite the onreceive (context, intent) callback function. Note the following four intentions:

Java code:

ACTION_APPWIDGET_UPDATEACTION_APPWIDGET_DELETEDACTION_APPWIDGET_ENABLEDACTION_APPWIDGET_DISABLED

Create an app widget configuration activity


If you want to adjust the settings when adding a new app widget, you can create an app widget configuration activity. This activity is automatically started by the app widget host and allows users to configure available settings when creating the activity, such as APP widget color, size, update cycle, or other function settings.


This configuration activity should be declared as a general activity in the android configuration file. However, it will be started by the app widget host through the action_appwidget_configure activity, so this activity needs to accept this intent. For example:

Java code:

< activity android:name=".ExampleAppWidgetConfigure">< intent-filter>< action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />< /intent-filter>< /activity>

Similarly, the activity must be declared in the appwidgetproviderinfo XML file through the Android: Configure attribute (see add appwidgetproviderinfo metadata adding above ).
The appwidgetproviderinfo metadata ). For example, the configuration activity can be declared as follows:

Java code:

< appwidget-provider xmlns:android=http://schemas.android.com/apk/res/android...android:configure="com.example.android.ExampleAppWidgetConfigure"... >< /appwidget-provider>

Note that this activity is declared by the full name because it will be referenced outside your package.


This is all you need to know about configuration activities at the beginning. Now you need a real activity. There are two important things to remember when you implement this activity:


The app widget host calls the configuration activity and the configuration activity always returns a result. this result should contain the app widget ID passed by the intent to start the activity (saved as extra_appwidget_id In the intent's additional segment intent extras)


When this app widget is created, the onupdate () method will not be called (when a configuration activity starts, the system will not send action_appwidget_update broadcast ). the configuration activity should request an update from appwidgetmanager when the app widget is created for the first time. however, onupdate () will be called in subsequent updates-only ignore the first time.


See the code snippets in the following sections. This example shows how to return a result from the configuration and update the app widget.


Update an app widget from the configuration activity

When an app widget uses a configuration activity, it should be used to update the app widget when the configuration is complete. You can directly request an update in appwidgetmanager to do this.

The following is a brief description of the process of updating the app widget and disabling the configuration activity:


1. First, obtain the app widget ID from the intent of starting this activity:


Java code:

Intent intent = getIntent();Bundle extras = intent.getExtras();if (extras != null) {mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);}

2. Implement your app widget configuration.
3. After the configuration is complete, call getinstance (context) to obtain an appwidgetmanager instance:

Java code:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

4. Call updateappwidget (INT,
Remoteviews) Update the app Widget:


Java code:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);appWidgetManager.updateAppWidget(mAppWidgetId, views);

5. Finally, create a return intent, set the activity result, and end the activity:

Java code:

Intent resultValue = new Intent();resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);setResult(RESULT_OK, resultValue);finish();

Tip: when your configuration activity is enabled for the first time, set the activity result to result_canceled. In this way, if the user returns from outside the activity before the end, the app
The widget host receives the configuration cancellation notification instead of adding the app widget.

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.