Skip to content

Instantly share code, notes, and snippets.

/MyWidget.java Secret
Created Oct 3, 2013

Embed
What would you like to do?
package com.example.mywidget;
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.view.View;
import android.widget.RemoteViews;
public class MyWidget extends AppWidgetProvider {
public static final String BTN_CLICKED = "buttonClick";
public static final String BTN_READY = "buttonReady";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.my_layout);
watchWidget = new ComponentName(context, MyWidget.class);
Intent intent = new Intent(context, getClass());
intent.setAction(BTN_CLICKED);
remoteViews.setOnClickPendingIntent(R.id.button,
PendingIntent.getBroadcast(context, 0, intent, 0));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if (BTN_CLICKED.equals(intent.getAction())) {
context.startService(new Intent(context, WidgetService.class));
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.my_layout);
watchWidget = new ComponentName(context, MyWidget.class);
remoteViews.setViewVisibility(R.id.button, View.GONE);
remoteViews.setViewVisibility(R.id.progressBar, View.VISIBLE);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
if (BTN_READY.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.my_layout);
watchWidget = new ComponentName(context, MyWidget.class);
remoteViews.setViewVisibility(R.id.button, View.VISIBLE);
remoteViews.setViewVisibility(R.id.progressBar, View.GONE);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.