Android Desktop Component App Widget Complete case
- 2020-08-22 22:39:49
- OfStack
This article illustrates the use of App Widget as an Android desktop component. Share to everybody for everybody reference. The details are as follows:
Here is a simulated example: after adding AppWidget to the desktop, click AppWidget and the AppWidget text will rotate
main.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Program entrance "
android:textSize="50dip"/>
</LinearLayout>
res/xml/ es18EN_appwidget.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="120dp"
android:minHeight="60dp"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/main">
</appwidget-provider>
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.activity" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<receiver android:name=".TestActivity">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/my_appwidget">
</meta-data>
<intent-filter>
<action android:name="COM.LJQ.ACTION.WIDGET.CLICK"></action>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
Variable class UtilTool: Used to control text changes:
package com.ljq.activity;
public class UtilTool {
public static boolean isChange=true;
}
TestActivity class, inherited from AppWidgetProvider:
package com.ljq.activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class TestActivity extends AppWidgetProvider {
// The custom 1 a Action The name
private static final String ACTION_CLICK_NAME = "COM.LJQ.ACTION.WIDGET.CLICK";
private RemoteViews rv;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
System.out.println("onUpdate");
// To obtain R.layout.main Layout through classes RemoteViews The layout R.layout.main Control in the operation
/*rv = new RemoteViews(context.getPackageName(), R.layout.main);
Intent intentClick = new Intent(ACTION_CLICK_NAME);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
rv.setOnClickPendingIntent(R.id.tv, pendingIntent);
ComponentName cmp = new ComponentName(context, TestActivity.class);
AppWidgetManager myAppWidgetManager = AppWidgetManager.getInstance(context);
myAppWidgetManager.updateAppWidget(cmp, rv);*/
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
//AppWidget The life cycle : Each receiving 1 Times, broadcast execution 1 Time for 1 The end of the life cycle.
// So I'm rewriting it AppWidgetProvider Class to declare global variables for state determination,
// Every state change AppWidgetProvider To receive the first 2 When you re-initialize it that means you re-instantiate it 1 time AppWidgetProvider .
// I put it in there today 1 a boolean Value initialized to true , observe the debugging and see that each entry is zero TRUE So when you set up your desktop components,
// Global variables declare it in the other 1 It is ok to use entity class to judge, do not put it in this class.
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive");
if (rv == null) {
rv = new RemoteViews(context.getPackageName(), R.layout.main);
}
if (intent.getAction().equals(ACTION_CLICK_NAME)) {
if (UtilTool.isChange) {
rv.setTextViewText(R.id.tv, "abc");
} else {
rv.setTextViewText(R.id.tv, "123");
}
UtilTool.isChange = !UtilTool.isChange;
AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(context);
int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(context, TestActivity.class));
appWidgetManger.updateAppWidget(appIds, rv);
}else{
super.onReceive(context, intent);
}
}
private void updateAppWidget(Context context,
AppWidgetManager appWidgeManger, int appWidgetId) {
rv = new RemoteViews(context.getPackageName(), R.layout.main);
Intent intentClick = new Intent();
intentClick.setAction(ACTION_CLICK_NAME);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
rv.setOnClickPendingIntent(R.id.tv, pendingIntent);
appWidgeManger.updateAppWidget(appWidgetId, rv);
}
}
I hope this article has been helpful for your Android programming.