我做了个appwidget 用listview绑定了列表 如何实现定时刷新,请高手指点!RemoteViewsService 代码
package com.example.samrun;
import entity.MyBean;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import java.util.ArrayList;import org.json.JSONArray;
import org.json.JSONObject;public class MyService extends RemoteViewsService { ArrayList<MyBean> mLists;
public IBinder onBind(Intent paramIntent)
{
return super.onBind(paramIntent);
} public void onCreate() {
super.onCreate();
Log.i("update", "onCreate in MyService..");
} public RemoteViewsService.RemoteViewsFactory onGetViewFactory(Intent paramIntent)
{
Log.i("update", "onGetViewFactory in MyService..");
return new MyRemoteViewsFactory(getApplicationContext(), paramIntent);
} private class MyRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
Context mContext; public MyRemoteViewsFactory(Context paramIntent, Intent arg3) {
this.mContext = paramIntent;
} public int getCount() {
return MyService.this.mLists.size();
} public long getItemId(int paramInt) {
return paramInt;
} public RemoteViews getLoadingView() {
return null;
} public RemoteViews getViewAt(int paramInt) {
Log.i("update", "getViewAt in MyRemoteViewsFactory..");
RemoteViews localRemoteViews = new RemoteViews(this.mContext.getPackageName(), R.layout.item_layout);
localRemoteViews.setTextViewText(R.id.title, ((MyBean)MyService.this.mLists.get(paramInt)).title);
localRemoteViews.setTextViewText(R.id.time, ((MyBean)MyService.this.mLists.get(paramInt)).intime);
Intent localIntent = new Intent();
localIntent.putExtra("id", ((MyBean)MyService.this.mLists.get(paramInt)).id);
localRemoteViews.setOnClickFillInIntent(R.id.item_llayout, localIntent);
return localRemoteViews;
} public int getViewTypeCount() {
return 0;
} public boolean hasStableIds() {
return false;
} public void onCreate() {
Log.i("update", "onCreate in MyRemoteViewsFactory..");
MyService localMyService = MyService.this;
String jsonArray;
jsonArray = "0";
ArrayList<MyBean> localArrayList = new ArrayList<MyBean>();
try
{
jsonArray = NetHelper.httpStringGet("");
if(!jsonArray.equals("0")){
Log.i("json", jsonArray);
JSONArray localJSONArray = new JSONArray(jsonArray);
for(int i = 0;i<localJSONArray.length();i++){
JSONObject localJSONObject = localJSONArray.getJSONObject(i);
MyBean localMyBean = new MyBean();
String str1 = localJSONObject.getString("id");
localMyBean.id = str1;
String str2 = localJSONObject.getString("title");
localMyBean.title = str2;
String str3 = localJSONObject.getString("intime");
localMyBean.intime = str3;
localArrayList.add(localMyBean);
}
}
}
catch (Exception localException)
{
localException.printStackTrace();
}
localMyService.mLists = localArrayList;
Log.i("update", "onCreate in MyService-----MyRemoteViewsFactory..");
} public void onDataSetChanged() {
} public void onDestroy() {
}
}
}
AppWidgetProvider 代码如下
package com.example.samrun;import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;public class MyAppWidgetProvider extends AppWidgetProvider {
public void onEnabled(Context paramContext)
{
super.onEnabled(paramContext);
}
public void onReceive(Context paramContext, Intent paramIntent)
{
super.onReceive(paramContext, paramIntent);
Log.i("update", "onReceive");
}
public void onUpdate(Context paramContext, AppWidgetManager paramAppWidgetManager, int[] paramArrayOfInt)
{
Log.i("update", "onUpdate");
super.onUpdate(paramContext, paramAppWidgetManager, paramArrayOfInt);
RemoteViews localRemoteViews = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout);
Intent localIntent = new Intent(paramContext, MyService.class);
localIntent.putExtra("appWidgetId", paramArrayOfInt[0]);
localRemoteViews.setRemoteAdapter(R.id.listView, localIntent);
localRemoteViews.setPendingIntentTemplate(R.id.listView, PendingIntent.getActivity(paramContext, 0, new Intent(paramContext, TargetActivity.class), 0));
paramAppWidgetManager.updateAppWidget(new ComponentName(paramContext, MyAppWidgetProvider.class), localRemoteViews);
}
}
listviewappwidget定时更新定时
package com.example.samrun;
import entity.MyBean;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import java.util.ArrayList;import org.json.JSONArray;
import org.json.JSONObject;public class MyService extends RemoteViewsService { ArrayList<MyBean> mLists;
public IBinder onBind(Intent paramIntent)
{
return super.onBind(paramIntent);
} public void onCreate() {
super.onCreate();
Log.i("update", "onCreate in MyService..");
} public RemoteViewsService.RemoteViewsFactory onGetViewFactory(Intent paramIntent)
{
Log.i("update", "onGetViewFactory in MyService..");
return new MyRemoteViewsFactory(getApplicationContext(), paramIntent);
} private class MyRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
Context mContext; public MyRemoteViewsFactory(Context paramIntent, Intent arg3) {
this.mContext = paramIntent;
} public int getCount() {
return MyService.this.mLists.size();
} public long getItemId(int paramInt) {
return paramInt;
} public RemoteViews getLoadingView() {
return null;
} public RemoteViews getViewAt(int paramInt) {
Log.i("update", "getViewAt in MyRemoteViewsFactory..");
RemoteViews localRemoteViews = new RemoteViews(this.mContext.getPackageName(), R.layout.item_layout);
localRemoteViews.setTextViewText(R.id.title, ((MyBean)MyService.this.mLists.get(paramInt)).title);
localRemoteViews.setTextViewText(R.id.time, ((MyBean)MyService.this.mLists.get(paramInt)).intime);
Intent localIntent = new Intent();
localIntent.putExtra("id", ((MyBean)MyService.this.mLists.get(paramInt)).id);
localRemoteViews.setOnClickFillInIntent(R.id.item_llayout, localIntent);
return localRemoteViews;
} public int getViewTypeCount() {
return 0;
} public boolean hasStableIds() {
return false;
} public void onCreate() {
Log.i("update", "onCreate in MyRemoteViewsFactory..");
MyService localMyService = MyService.this;
String jsonArray;
jsonArray = "0";
ArrayList<MyBean> localArrayList = new ArrayList<MyBean>();
try
{
jsonArray = NetHelper.httpStringGet("");
if(!jsonArray.equals("0")){
Log.i("json", jsonArray);
JSONArray localJSONArray = new JSONArray(jsonArray);
for(int i = 0;i<localJSONArray.length();i++){
JSONObject localJSONObject = localJSONArray.getJSONObject(i);
MyBean localMyBean = new MyBean();
String str1 = localJSONObject.getString("id");
localMyBean.id = str1;
String str2 = localJSONObject.getString("title");
localMyBean.title = str2;
String str3 = localJSONObject.getString("intime");
localMyBean.intime = str3;
localArrayList.add(localMyBean);
}
}
}
catch (Exception localException)
{
localException.printStackTrace();
}
localMyService.mLists = localArrayList;
Log.i("update", "onCreate in MyService-----MyRemoteViewsFactory..");
} public void onDataSetChanged() {
} public void onDestroy() {
}
}
}
AppWidgetProvider 代码如下
package com.example.samrun;import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;public class MyAppWidgetProvider extends AppWidgetProvider {
public void onEnabled(Context paramContext)
{
super.onEnabled(paramContext);
}
public void onReceive(Context paramContext, Intent paramIntent)
{
super.onReceive(paramContext, paramIntent);
Log.i("update", "onReceive");
}
public void onUpdate(Context paramContext, AppWidgetManager paramAppWidgetManager, int[] paramArrayOfInt)
{
Log.i("update", "onUpdate");
super.onUpdate(paramContext, paramAppWidgetManager, paramArrayOfInt);
RemoteViews localRemoteViews = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout);
Intent localIntent = new Intent(paramContext, MyService.class);
localIntent.putExtra("appWidgetId", paramArrayOfInt[0]);
localRemoteViews.setRemoteAdapter(R.id.listView, localIntent);
localRemoteViews.setPendingIntentTemplate(R.id.listView, PendingIntent.getActivity(paramContext, 0, new Intent(paramContext, TargetActivity.class), 0));
paramAppWidgetManager.updateAppWidget(new ComponentName(paramContext, MyAppWidgetProvider.class), localRemoteViews);
}
}
listviewappwidget定时更新定时
解决方案 »
- 求几本android的进阶实战书籍名
- android如何画一条动态直线?
- android应用的用户使用情况统计如何做?
- AutoCompleteTextView 实现键值对
- onLocationChanged方法结束后自动关闭gps
- 怎么判断sd卡中的歌曲文件更新了?或者mediastore更新了?
- WebService异常
- 请问我这样在R文件 添加一个ID有错吗?为什么R文件里面没有显示我所添加的ID
- 谁知道有没有自动生成代码的插件
- 一直出现这个#include expects "FILENAME" or <FILENAME>,大佬求帮助呀,小白真的看不懂
- 奇怪的页面控件更新问题,求教了.......
- 有关android /data/data/ [
这个方法是怎么实现的!! 我 也在研究这个问题 ! 有时间的话 加qq1204101952
还有就是第一次加载时 一直是:“正在加载..”遇到过没!!