AsynTask With RemoteViews In StackView Widget
Solution 1:
You can use the method onDataSetChanged in class RSSRemoteViewsFactory. This is triggered when you call AppWidgetManager notifyAppWidgetViewDataChanged on the collection view corresponding to this factory. You can do heaving lifting in here, synchronously. For example, if you need to process an image, fetch something from the network, etc., it is ok to do it here, synchronously. The widget will remain in its current state while work is being done here, so you don't need to worry about locking up the widget.
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private final String TAG = StackRemoteViewsFactory.class.getSimpleName();
private Context mContext;
private int mAppWidgetId;
private List<Game> mGameItems = new ArrayList<Game>();
public StackRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
public void onCreate() {
}
public void onDestroy() {
mGameItems.clear();
}
public int getCount() {
return mGameItems.size();
}
public RemoteViews getViewAt(int position) {
Game game = mGameItems.get(position);
Bundle extras = new Bundle();
extras.putString(StackWidgetProvider.EXTRA_ITEM, game.getGameUrl());
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
Bitmap bitmap = null;
try {
URL imageUrl = new URL(game.getImageUrl());
bitmap = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
rv.setImageViewBitmap(R.id.widget_item, bitmap);
return rv;
}
public RemoteViews getLoadingView() {
return null;
}
public int getViewTypeCount() {
return 1;
}
public long getItemId(int position) {
return position;
}
public boolean hasStableIds() {
return true;
}
public void onDataSetChanged() {
final String uri = <set your url>
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(uri);
String jsonData = null;
/**
* Fetch JSON from web service
* TODO: try to call a AsyncTask
*/
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder data = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
data.append(line);
}
jsonData = new String(data);
Log.d(TAG, String.format("JSON: %s", jsonData));
} catch (IOException e) {
e.printStackTrace();
}
/**
* Mapping JSON String to objects
* TODO: Probably move the code to a dedicated class and use GSON to do the mapping?????
*/
if(jsonData != null && !jsonData.equals("")) {
try {
JSONArray jsonArray = new JSONArray(jsonData);
for(int index = 0; index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
String id = jsonObject.getString("id");
String imageUrl = jsonObject.getString("imageUrl");
String gameUrl = jsonObject.getString("gameUrl");
mGameItems.add(
new Game(id, imageUrl, gameUrl)
);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.d(TAG, String.format("List of games: %s", mGameItems));
}
}
In the following example, I'm fetching a json from a webserver and then I fetch the images from each item. The onDataSetChanged is used to fetch the json content and the in the getViewAt I fetch the image.
Check the comments at the StackView example provided by Google - http://docs.huihoo.com/android/3.0/resources/samples/StackWidget/index.html
Hope it helps you
Post a Comment for "AsynTask With RemoteViews In StackView Widget"