正如标题所说,我想要构建的是一个显示特定REST服务响应的android 小部件。我是android的新手。因此,我使用this great tutorial来构建窗口小部件并从android服务更新它。然后我使用基于this post的volley library来从REST服务器检索数据。以下是我的服务OnStart
方法的代码:
@Override
public void onStart(Intent intent, int startId) {
// create some random data
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),
MyWidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
applicationContext = this.getApplicationContext();
for (int widgetId : allWidgetIds) {
number = (new Random().nextInt(100));
Log.w("WidgetExample", "Showing:" + String.valueOf(number));
remoteViews = new RemoteViews(applicationContext.getPackageName(),
R.layout.widget_layout);
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));
/////////////////////////////////////////////////////////
/*// Get the data from the rest service
RequestQueue queue = Volley.newRequestQueue(this);
// String url =
// "https://www.googleapis.com/customsearch/v1?key=AIzaSyBmSXUzVZBKQv9FJkTpZXn0dObKgEQOIFU&cx=014099860786446192319:t5mr0xnusiy&q=AndroidDev&alt=json&searchType=image";
String url = "http://develop1.med.auth.gr:8080/envapp/json?date=2014-3-14";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.w("WidgetExample",
"Response => " + response.toString());
// Set the text\
remoteViews.setTextViewText(R.id.update,
"Response => " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.w("WidgetExample",
"Error => " + error.toString());
remoteViews.setTextViewText(R.id.update,
"Error => " + error.toString());
}
});
queue.add(jsObjRequest);*/
////////////////////////////////////////////////////////////////////////////
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(),
MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
如您所见,我已经注释掉了REST服务调用代码。这个片段,播放得很好。它使用随机数更新UI。如果我取消注释REST服务调用代码,我可以在LogCat中看到服务的响应,但我无法更新UI 。我尝试过同步调用来获取REST服务响应,但没有运气。
有人可以告诉我为什么我无法从响应事件处理程序更新UI吗?这可能吗?我怎么能这样做?有人能给我指教吗?
答案 0 :(得分:1)
我发现了问题所在。我必须调用updateAppWidget
方法。该方法应如下......
@Override
public void onStart(Intent intent, int startId) {
appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),
MyWidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
for (int widgetId : allWidgetIds) {
number = (new Random().nextInt(100));
widgetIdToUse = widgetId;
Log.w("WidgetExample", "Showing:" + String.valueOf(number));
remoteViews = new RemoteViews(this.getApplicationContext()
.getPackageName(), R.layout.widget_layout);
// Get the data from the rest service
RequestQueue queue = Volley.newRequestQueue(this);
// String url =
// "https://www.googleapis.com/customsearch/v1?key=AIzaSyBmSXUzVZBKQv9FJkTpZXn0dObKgEQOIFU&cx=014099860786446192319:t5mr0xnusiy&q=AndroidDev&alt=json&searchType=image";
String url = "http://develop1.med.auth.gr:8080/envapp/json?date=2014-3-14";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.w("WidgetExample",
"Response => " + response.toString());
// Set the text\
try {
remoteViews.setTextViewText(R.id.update,
response.getString("gentoday"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
appWidgetManager.updateAppWidget(widgetIdToUse, remoteViews);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.w("WidgetExample",
"Error => " + error.toString());
remoteViews.setTextViewText(R.id.update,
"Error => " + error.toString());
appWidgetManager.updateAppWidget(widgetIdToUse, remoteViews);
}
});
queue.add(jsObjRequest);
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(),
MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}