我想从Web服务获取数据并在小部件中显示。
我执行以下代码,但在获取数据后widget未更新。我搜索了很多,但没有得到任何帮助。
想要从服务器获取数据并在小部件中显示。我得到了数据,但它没有在小部件上显示。哪里错了?
请参阅以下代码。
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="146dp"
android:minWidth="292dp"
android:updatePeriodMillis="86400"
>
</appwidget-provider>
public class MyWidgetProvider extends AppWidgetProvider {
ArrayList<String> aryThumbnail = new ArrayList<String>();
ArrayList<String> aryUrl = new ArrayList<String>();
ArrayList<String> aryTitle = new ArrayList<String>();
ArrayList<String> aryDesc = new ArrayList<String>();
ArrayList<String> aryId = new ArrayList<String>();
ArrayList<String> aryLike = new ArrayList<String>();
ArrayList<String> aryTime = new ArrayList<String>();
Context context;
RemoteViews views;
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
new AsyncAction().execute(null, null, null);
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
this.context = context;
for (int i = 0; i < appWidgetIds.length; i++) {
int currentWidgetId = appWidgetIds[i];
String url = "http://www.tutorialspoint.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url));
PendingIntent pending = PendingIntent.getActivity(context, 0,
intent, 0);
views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.button1, pending);
Intent configIntent = new Intent(context, HomeActivity.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(
context, 0, configIntent, 0);
views.setOnClickPendingIntent(R.id.widget, configPendingIntent);
Toast.makeText(context, "widget added", Toast.LENGTH_SHORT).show();
appWidgetManager.updateAppWidget(currentWidgetId, views);
}
}
private class AsyncAction extends AsyncTask<String, Void, String> {
protected String doInBackground(String... arg0) {
callRecentVideo();
// populateListItem();
return null;
}
protected void onPostExecute(String result) {
}
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
public static void updateAppWidget(Context context,
AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
@SuppressLint("NewApi")
private String callRecentVideo() {
String result;
DefaultHttpClient httpclient = new DefaultHttpClient();
@SuppressWarnings("static-access")
Function c = new Function();
result = c.getString(c.link.toString() + "recent_videos");
try {
JSONObject json_data = new JSONObject(result);
String getStatus = json_data.getString("status");
Log.e("status", getStatus);
String getError = json_data.getString("ERROR");
Log.e("Error", getError);
String getReason = json_data.getString("Reason");
// Log.e("Reason", getReason);
if (getError == null) {
// return "Unable to SignIn, please try again.";
return getReason;
}
String getDetails = json_data.getString("videos");
// Log.e("videos", getDetails);
JSONArray aryVideo = new JSONArray(getDetails);
for (int i = 0; i <= aryVideo.length(); i++) {
String id = aryVideo.getJSONObject(i).getString("id");
String channel_id = aryVideo.getJSONObject(i).getString(
"channel_id");
String videos_title = aryVideo.getJSONObject(i).getString(
"videos_title");
String description = aryVideo.getJSONObject(i).getString(
"description");
String thumbnail = aryVideo.getJSONObject(i).getString(
"thumbnail");
String url = aryVideo.getJSONObject(i).getString("url");
String likes = aryVideo.getJSONObject(i).getString("likes");
String uploadtime = aryVideo.getJSONObject(i).getString(
"uploadtime");
aryId.add(id.toString());
aryThumbnail.add(thumbnail.toString());
aryUrl.add(url.toString());
aryTitle.add(videos_title.toString());
views.setTextViewText(R.id.title, videos_title);
}
} catch (Exception e) {
}
return null;
}