3
您好新來的android小部件,我搜索的例子來檢索溫度(其服務),以appwidget提供,但我沒有明確的想法。所以我開始一個服務來獲取溫度細節。如何檢索溫度值(從服務)到AppWidgetProvider
它我的服務(在這裏我得到了溫度值)需要知道我怎麼想這個detials傳遞到的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);
}
}
請檢查上面的代碼和幫我完成這個任務