Android Listivew小部件未自动更新
我的朋友已经完成了一个应用程序,现在他为他的应用程序创建了一个小部件,可以用来以列表视图的方式显示任务列表。首先,当他拖动小部件并在主屏幕中安装时,它将正常工作,并以列表视图的方式显示任务 他的问题是,当他在应用程序中进行更改,然后返回主屏幕时,没有反映出任何更改。他正在使用Android Listivew小部件未自动更新,android,widget,auto-update,Android,Widget,Auto Update,我的朋友已经完成了一个应用程序,现在他为他的应用程序创建了一个小部件,可以用来以列表视图的方式显示任务列表。首先,当他拖动小部件并在主屏幕中安装时,它将正常工作,并以列表视图的方式显示任务 他的问题是,当他在应用程序中进行更改,然后返回主屏幕时,没有反映出任何更改。他正在使用onReceive()方法更新小部件 import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.a
onReceive()
方法更新小部件
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.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
public class WidgetTaskSchedular extends AppWidgetProvider {
private final String TAG = "CalendarViewSample:"
+ this.getClass().getName();
SharedPreferences sharedprefer;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getAction().equals("update_widget"))
{
int[] appid=new int[1];
sharedprefer=context.getSharedPreferences("TaskWidget", 0);
appid[0]=sharedprefer.getInt("widget_key",0);
Log.i(TAG,"Appwidgetid"+appid[0]);
onUpdate(context, AppWidgetManager.getInstance(context),appid);
}
}
public static String EXTRA_WORD=
"com.capsone.testing.calendar.WORD";
@SuppressWarnings("deprecation")
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i=0; i<appWidgetIds.length; i++) {
sharedprefer=context.getSharedPreferences("TaskWidget", 0);
SharedPreferences.Editor editor=sharedprefer.edit();
editor.putInt("widget_key", appWidgetIds[i]);
editor.commit();
int a= sharedprefer.getInt("widget_key", 0);
Log.i(TAG,"Sharedpreferencewidgetid:"+a);
//ID=appWidgetIds[i];
Log.i(TAG,"LengthofWidget:"+appWidgetIds.length);
// Log.i(TAG,"TestAppWidget:"+ID);
Intent intentWidgetService=new Intent(context, WidgetService.class);
intentWidgetService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intentWidgetService.setData(Uri.parse(intentWidgetService.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteView=new RemoteViews(context.getPackageName(),
R.layout.widgetlayout);
remoteView.setRemoteAdapter(appWidgetIds[i], R.id.listWidget,
intentWidgetService);
Intent clickIntent=new Intent(context, ActionBarActivity.class);
PendingIntent clickPendingIntent=PendingIntent
.getActivity(context, 0,
clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setPendingIntentTemplate(R.id.listWidget, clickPendingIntent);
ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
appWidgetManager.updateAppWidget(component, remoteView);
}
}
}
导入android.app.pendingent;
导入android.appwidget.AppWidgetManager;
导入android.appwidget.AppWidgetProvider;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.content.SharedReferences.Editor;
导入android.net.Uri;
导入android.util.Log;
导入android.widget.remoteview;
公共类WidgetTaskSchedular扩展了AppWidgetProvider{
私有最终字符串标记=“CalendarViewSample:”
+这个.getClass().getName();
SharedReferences SharedReferer;
@凌驾
公共void onReceive(上下文、意图){
super.onReceive(上下文、意图);
if(intent.getAction().equals(“更新_小部件”))
{
int[]appid=新int[1];
sharedprefer=context.getSharedPreferences(“TaskWidget”,0);
appid[0]=sharedprefer.getInt(“小部件密钥”,0);
Log.i(标记“Appwidgetid”+appid[0]);
onUpdate(上下文,AppWidgetManager.getInstance(上下文),appid);
}
}
公共静态字符串附加字=
“com.capsone.testing.calendar.WORD”;
@抑制警告(“弃用”)
@凌驾
public void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,
int[]appWidgetIds){
onUpdate(上下文、appWidgetManager、AppWidgetId);
对于(int i=0;i您是否尝试过此处提到的解决方案:
请注意,以上只是触发更新,小部件本身将需要获取所需的数据
请注意,虽然广播接收器可以在小部件中注册,但在更新后,它们将进行垃圾收集,因此,这不是一个可靠的解决方案
最后一种选择是注册AlarmManager以启动执行小部件更新的服务。我建议您尝试粘贴以下代码:
ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listWidget);
appWidgetManager.updateAppWidget(component, remoteView);
notifyAppWidgetViewDataChanged()
因此,当您调用notifyAppWidgetViewDataChanged()
时
将调用RemoteViewsFactory
的方法,该方法用作ListView
的适配器。您可以在onDataSetChanged()中执行web服务/网络相关操作、从数据库获取数据操作和其他操作
,获取响应并将其添加到您的数据集,该数据集可能是ArrayList
或任何此类集合
你可以参考