Guest User

Untitled

a guest
Jan 27th, 2020
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. From my Fragment I send:
  2. public void sendToWidget(Subreddit subreddit) {
  3.         Context context = getActivity();
  4.         currentSubreddit = subreddit;
  5.  
  6.         if (context != null) {
  7.             SharedPreferences prefs = context.getSharedPreferences("widget_prefs", Context.MODE_PRIVATE);
  8.             SharedPreferences.Editor editor = prefs.edit();
  9.             editor.putString("name", currentSubreddit.getName());
  10.             editor.commit();
  11.             Log.d(TAG, "sendToWidget: " + subreddit.getName());
  12.         }
  13.     }
  14. //-----------------------------------------------------
  15.  
  16. Here's my Service:
  17.   @Override
  18.    public int onStartCommand(Intent intent, int flags, int startId) {
  19.  
  20.        RemoteViews views = getPrefs(this);
  21.        ComponentName theWidget = new ComponentName(this, TrendyWidgetProvider.class);
  22.        AppWidgetManager manager = AppWidgetManager.getInstance(this);
  23.        manager.updateAppWidget(theWidget, views);
  24.        Log.d(TAG, "onStartCommand: " + mSubreddit.getName());
  25.        return super.onStartCommand(intent, flags, startId);
  26.    }
  27.  
  28.   private RemoteViews getPrefs(Context context) {
  29.        mSubreddit = new Subreddit();
  30. //        context = getApplicationContext();
  31.            SharedPreferences prefs = context.getSharedPreferences("widget_prefs", Context.MODE_PRIVATE);
  32.            String imgUrl = prefs.getString("icon", mSubreddit.getIconUrl());
  33.            String name = prefs.getString("title", mSubreddit.getName());
  34.            String lastUpdate = "Last subreddit added: " + "\n" + name;
  35.            RemoteViews views = new RemoteViews(getPackageName(), R.layout.trendy_widget);
  36.            views.setTextViewText(R.id.trendy_title, lastUpdate);
  37.  
  38.        Log.d(TAG, "dataReceived: " + name);
  39.        return views;
  40.    }
  41.  
  42. And here's my WidgetProvider
  43.  
  44.     static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
  45.                                 int appWidgetId) {
  46.  
  47.         // Construct the RemoteViews object
  48.         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.trendy_widget);
  49.         AppWidgetTarget target = new AppWidgetTarget(context,80, 80, R.id.trendy_img, views, appWidgetId);
  50.         views.setTextViewText(R.id.trendy_title, Integer.toString(mCounter));
  51.         appWidgetManager.updateAppWidget(appWidgetId, views);
  52.     }
  53.  
  54.     @Override
  55.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  56.  
  57.         for (int appWidgetId : appWidgetIds) {
  58.             updateAppWidget(context, appWidgetManager, appWidgetId);
  59.         }
  60.         context.startService(new Intent(context, UpdateIntentService.class));
  61.  
  62.         Intent intent = new Intent(context, SearchActivity.class);
  63.  
  64.         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  65.  
  66.         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.trendy_widget);
  67.  
  68.         views.setOnClickPendingIntent(R.id.widget_container, pendingIntent);
  69.  
  70.         appWidgetManager.updateAppWidget(appWidgetIds, views);
  71.     }
RAW Paste Data