Created
December 18, 2014 01:51
-
-
Save thenrich/198a513f516fdc5252aa to your computer and use it in GitHub Desktop.
GateOpener.java Widget for Android Gate Opener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package YOURPACKAGE.gateopener; | |
import android.app.PendingIntent; | |
import android.appwidget.AppWidgetManager; | |
import android.appwidget.AppWidgetProvider; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.util.Log; | |
import android.widget.RemoteViews; | |
/** | |
* Implementation of App Widget functionality. | |
*/ | |
public class GateOpener extends AppWidgetProvider { | |
public static String WIDGET_BUTTON = "YOURPACKAGE.gateopener.WIDGET_BUTTON"; | |
@Override | |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { | |
// Update all active widgets | |
final int N = appWidgetIds.length; | |
for (int i=0; i<N; i++) { | |
updateAppWidget(context, appWidgetManager, appWidgetIds[i]); | |
} | |
} | |
@Override | |
public void onEnabled(Context context) { | |
// Used when the widget is first created | |
} | |
@Override | |
public void onDisabled(Context context) { | |
// Used when widget is disabled | |
} | |
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, | |
int appWidgetId) { | |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.gate_opener); | |
Intent intent = new Intent(WIDGET_BUTTON); | |
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
views.setOnClickPendingIntent(R.id.btnOpen, pendingIntent); | |
appWidgetManager.updateAppWidget(appWidgetId, views); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment