gpt4 book ai didi

android - 如何将温度值(从服务)检索到 AppWidgetProvider

转载 作者:太空狗 更新时间:2023-10-29 13:15:18 25 4
gpt4 key购买 nike

您好,我是 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);
}
}

请检查上面的代码并帮助我完成此任务

最佳答案

服务中:

            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);

}

关于android - 如何将温度值(从服务)检索到 AppWidgetProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com