tutorial app widget android

Solutions on MaxInterview for tutorial app widget android by the best coders in the world

showing results for - "tutorial app widget android"
Paul
06 Feb 2020
1package de.vogella.android.widget.example;
2
3import java.util.Random;
4
5import android.app.PendingIntent;
6import android.appwidget.AppWidgetManager;
7import android.appwidget.AppWidgetProvider;
8import android.content.ComponentName;
9import android.content.Context;
10import android.content.Intent;
11import android.util.Log;
12import android.widget.RemoteViews;
13
14public class MyWidgetProvider extends AppWidgetProvider {
15
16    private static final String ACTION_CLICK = "ACTION_CLICK";
17
18    @Override
19    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
20            int[] appWidgetIds) {
21
22        // Get all ids
23        ComponentName thisWidget = new ComponentName(context,
24                MyWidgetProvider.class);
25        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
26        for (int widgetId : allWidgetIds) {
27            // create some random data
28            int number = (new Random().nextInt(100));
29
30            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
31                    R.layout.widget_layout);
32            Log.w("WidgetExample", String.valueOf(number));
33            // Set the text
34            remoteViews.setTextViewText(R.id.update, String.valueOf(number));
35
36            // Register an onClickListener
37            Intent intent = new Intent(context, MyWidgetProvider.class);
38
39            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
40            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
41
42            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
43                    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
44            remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
45            appWidgetManager.updateAppWidget(widgetId, remoteViews);
46        }
47    }
48}