如何将温度值(从服务)检索到 AppWidgetProvider
Posted
技术标签:
【中文标题】如何将温度值(从服务)检索到 AppWidgetProvider【英文标题】:How to retrieve the temperature value (from service) to AppWidgetProvider 【发布时间】:2016-05-24 11:05:14 【问题描述】:嗨,我是 android 小部件的新手,我搜索了示例以检索 appwidget 提供的温度(它在服务中),但我对此并不清楚。因此,我启动一项服务以获取温度详细信息。
它是我的服务(这里我得到了临时值)需要知道我想如何将此详细信息传递给 AppWidgetProvider
class MainService extends Service
private SharedPreferences sharedPref;
private static final String Sync_My_Settings = "SyncSettings";
private Button tempbut;
public static ArrayList<HashMap<String, String>> mDataFetchList;
private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
@Override
public void onCreate()
super.onCreate();
sharedPref = MainService.this.getSharedPreferences(Sync_My_Settings, Context.MODE_PRIVATE);
setCurrentLocation();
Log.e("edgar","Started Service");
@Nullable
@Override
public IBinder onBind(Intent intent)
return null;
public void setCurrentLocation()
String myLat= "myLat",myLong="myLong";
loc1 = new GeoLocationUtil(this).getCurrentLocation(this);
if (loc1 != null)
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(myLat, String.valueOf(loc1.getLatitude()));
editor.putString(myLong, String.valueOf(loc1.getLongitude()));
editor.commit();
System.out.println("PWF Latitude = " + String.valueOf(loc1.getLatitude()) + "Longitude = " + String.valueOf(loc1.getLongitude()));
JSONWeatherTask task = new JSONWeatherTask();
task.execute("", sharedPref.getString(myLat, "0"), sharedPref.getString(myLong, "0"));
try
int myTemp = (int) task.get().temperature.getTemp();
String City =task.get().location.getCity();
//System.out.println("SWF HH display fd=" + result);
mDataFetchList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("Temp", City);
mDataFetchList.add(map);
Intent widgetUpdateIntent = new Intent();
widgetUpdateIntent.setAction(MyProvider.DATA_FETCHED);
widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
sendBroadcast(widgetUpdateIntent);
catch (InterruptedException | ExecutionException e)
e.printStackTrace();
else
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
private class JSONWeatherTask extends AsyncTask<String, Void, Weather>
@Override
protected Weather doInBackground(String... params)
Weather weather = new Weather();
String data = ( (new WeatherHttpClient()).getWeatherData(params[0], params[1], params[2]));
try
weather = JSONWeatherParser.getWeather(data);
// Let's retrieve the icon
weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));
catch (JSONException e)
e.printStackTrace();
return weather;
@Override
protected void onPostExecute(Weather weather)
super.onPostExecute(weather);
if (weather.iconData != null && weather.iconData.length > 0)
Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length);
imgView.setImageBitmap(img);
String City =weather.location.getCity();
String Cloudy =(weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
String temper ="" + weather.temperature.getTemp() + "\u00B0C";
还有我的 AppWidgetProvide
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
final int count = appWidgetIds.length;
for (int i = 0; i < count; i++)
final Intent intentt = new Intent(context, MainService.class);
intentt.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
context.startService(intentt);
super.onUpdate(context, appWidgetManager, appWidgetIds);
@Override
public void onEnabled(Context context)
// TODO Auto-generated method stub
super.onEnabled(context);
@Override
public void onDisabled(Context context)
// TODO Auto-generated method stub
super.onDisabled(context);
private RemoteViews updateWidgetListView(Context context, int appWidgetId)
// which layout to show on widget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.simple_widgett);
// RemoteViews Service needed to provide adapter for ListView
Intent svcIntent = new Intent(context, MainService.class);
// passing app widget id to that RemoteViews Service
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// setting a unique Uri to the intent
if (MainService.mDataFetchList != null)
svcIntent.putExtra("data", MainService.mDataFetchList);
remoteViews.setTextViewText(R.id.btn1, "Temp");
remoteViews.setRemoteAdapter(appWidgetId, R.id.btn1, svcIntent);
remoteViews.setTextViewText(R.id.am, am.format(new Date()));
Intent toastIntent = new Intent(context, MyProvider.class);
toastIntent.setAction(MyProvider.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.date, toastPendingIntent);
return remoteViews;
@Override
public void onReceive(Context context, Intent intent)
super.onReceive(context, intent);
if (intent.getAction().equals(DATA_FETCHED))
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
// update and notify widget when data arrives
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.date);
请检查上面的代码并帮助我完成这项任务
【问题讨论】:
【参考方案1】:服务中:
Intent intent = new Intent(this, MyProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra("City", City);
intent.putExtra("Temp", myTemp);
sendBroadcast(intent);
在 AppWidgetProvider 类中
@Override
public void onReceive(Context context, Intent intent)
super.onReceive(context, intent);
Bundle bundle = intent.getExtras();
if (bundle != null)
City = bundle.getString("City");
Temperature = bundle.getInt("Temp");
int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
appWidgetManager.updateAppWidget(currentWidgetId, remoteViews);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,
R.id.date);
【讨论】:
以上是关于如何将温度值(从服务)检索到 AppWidgetProvider的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 StreamBuilder 将数据从 firebase firestore 检索到颤振中,并使用 ListView.builder 显示值?