Created
July 29, 2016 20:09
-
-
Save the-dagger/7ade916e1ce3015fed44f30d2a38b775 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import android.annotation.TargetApi; | |
| import android.app.PendingIntent; | |
| import android.appwidget.AppWidgetManager; | |
| import android.appwidget.AppWidgetProvider; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.os.Build; | |
| import android.support.annotation.NonNull; | |
| import android.widget.RemoteViews; | |
| /** | |
| * Implementation of App Widget functionality. | |
| */ | |
| public class StockWidgetProvider extends AppWidgetProvider { | |
| private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, | |
| int appWidgetId) { | |
| // Construct the RemoteViews object which defines the view of out widget | |
| RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); | |
| // Instruct the widget manager to update the widget | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { | |
| setRemoteAdapter(context, views); | |
| } else { | |
| setRemoteAdapterV11(context, views); | |
| } | |
| /** PendingIntent to launch the MainActivity when the widget was clicked **/ | |
| Intent launchMain = new Intent(context, MainActivity.class); | |
| PendingIntent pendingMainIntent = PendingIntent.getActivity(context, 0, launchMain, 0); | |
| views.setOnClickPendingIntent(R.id.widget, pendingMainIntent); | |
| appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,R.id.widget_listView); | |
| appWidgetManager.updateAppWidget(appWidgetId, views); | |
| } | |
| @Override | |
| public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { | |
| // There may be multiple widgets active, so update all of them | |
| for (int appWidgetId : appWidgetIds) { | |
| updateAppWidget(context, appWidgetManager, appWidgetId); | |
| } | |
| super.onUpdate(context, appWidgetManager, appWidgetIds); | |
| } | |
| @Override | |
| public void onEnabled(Context context) { | |
| // Enter relevant functionality for when the first widget is created | |
| } | |
| @Override | |
| public void onDisabled(Context context) { | |
| // Enter relevant functionality for when the last widget is disabled | |
| } | |
| /** Set the Adapter for out widget **/ | |
| @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) | |
| private static void setRemoteAdapter(Context context, @NonNull final RemoteViews views) { | |
| views.setRemoteAdapter(R.id.widget_listView, | |
| new Intent(context, StockWidgetService.class)); | |
| } | |
| /** Deprecated method, don't create this if you are not planning to support devices below 4.0 **/ | |
| @SuppressWarnings("deprecation") | |
| private static void setRemoteAdapterV11(Context context, @NonNull final RemoteViews views) { | |
| views.setRemoteAdapter(0, R.id.widget_listView, | |
| new Intent(context, StockWidgetService.class)); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment