mirror of
https://github.com/PranshulGG/WeatherMaster.git
synced 2026-10-23 03:24:11 -05:00
updated widgets provider
This commit is contained in:
1 parent
aa4040a43b
commit
eb50f495ef
3 files changed
+230
No files matched your search
@@ -0,0 +1,75 @@
|
||||
package com.example.weathermaster;
|
||||
|
||||
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.content.SharedPreferences;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
public class WidgetProviderPill extends AppWidgetProvider {
|
||||
|
||||
private static final String PREFS_NAME = "WeatherWidgetPrefsPill";
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||
|
||||
// Retrieve persisted data
|
||||
String mainTemp = prefs.getString("mainTemp", "--°");
|
||||
String iconData = prefs.getString("iconData", "cloudy");
|
||||
|
||||
|
||||
for (int appWidgetId : appWidgetIds) {
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_weather_pill);
|
||||
|
||||
// Set widget data
|
||||
int iconResId = context.getResources().getIdentifier(iconData, "drawable", context.getPackageName());
|
||||
|
||||
|
||||
views.setImageViewResource(R.id.weather_icon_pill, iconResId);
|
||||
views.setTextViewText(R.id.temp_text_pill, mainTemp);
|
||||
|
||||
|
||||
// Add click listener to open MainActivity
|
||||
Intent intent = new Intent(context, MainActivity.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
|
||||
views.setOnClickPendingIntent(R.id.widget_layout_pill, pendingIntent);
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateWeatherWidgetPill(Context context, String mainTemp, String iconData) {
|
||||
|
||||
// Save data to SharedPreferences
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
|
||||
editor.putString("mainTemp", mainTemp);
|
||||
editor.putString("iconData", iconData);
|
||||
|
||||
|
||||
editor.apply();
|
||||
|
||||
// Update widget views
|
||||
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
ComponentName widgetComponent = new ComponentName(context, WidgetProviderPill.class);
|
||||
int[] widgetIds = appWidgetManager.getAppWidgetIds(widgetComponent);
|
||||
|
||||
for (int widgetId : widgetIds) {
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_weather_pill);
|
||||
|
||||
int iconResId = context.getResources().getIdentifier(iconData, "drawable", context.getPackageName());
|
||||
|
||||
|
||||
views.setImageViewResource(R.id.weather_icon_pill, iconResId);
|
||||
views.setTextViewText(R.id.temp_text_pill, mainTemp);
|
||||
|
||||
|
||||
appWidgetManager.updateAppWidget(widgetId, views);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user