我在窗口小部件上有两个按钮可以更改窗口小部件中的某些项目,如果在手机上更改了方向,按钮不起作用.我读了
http://developer.android.com/guide/topics/resources/runtime-changes.html,但这是关于活动而不是小部件.
@H_403_2@@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
Intent active = new Intent(context,TvWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
mDbHelper = new DbAdapter(context);
fillChannelList(context,appWidgetIds[appWidgetIds.length-1]);
Set<Integer> keys = channelsImages.keySet();
Iterator<Integer> iter = keys.iterator();
while(iter.hasNext())
{
if(channelId == 0)
{
channelId = iter.next();
break;
}
}
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME,0);
Editor edit = settings.edit();
edit.putInt("channelId",channelId);
edit.putInt("appWidgetIds",appWidgetIds[appWidgetIds.length-1]);
edit.commit();
active.putExtra("net.aerosoftware.tvvodic.appWidgetIds",appWidgetIds);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,active,0);
remoteViews.setOnClickPendingIntent(R.id.button_next,actionPendingIntent);
Intent refresh = new Intent(context,TvWidget.class);
refresh.setAction(ACTION_WIDGET_REFRESH);
refresh.putExtra("net.aerosoftware.tvvodic.appWidgetIds",appWidgetIds);
PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context,refresh,0);
remoteViews.setOnClickPendingIntent(R.id.button_refresh,refreshPendingIntent);
updateView(context);
appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);
super.onUpdate(context,appWidgetManager,appWidgetIds);
}
解决方法
我建议创建一个服务(可能在您的AppWidgetProvider中进行子类化)并覆盖onConfigurationChanged()方法.使用该服务将允许您委派业务逻辑来处理该服务,构建和更新您的窗口小部件.它也将允许您管理轮换.如果您执行任何阻塞操作,那么该服务将是产生线程并将结果返回主UI线程以避免ANR的好地方.
我会建议如下:
@H_403_2@public class MyWidget extends AppWidgetProvider { @Override public void onUpdate(Context context,int[] appWidgetIds) { context.startService(new Intent(context,MyUpdateService.class)); } public static class MyUpdateService extends Service { @Override public void onStart(Intent intent,int startId) { super.onStart(intent,startId); // Update the widget RemoteView remoteView = buildRemoteView(this); // Push update to homescreen pushUpdate(remoteView); // No more updates so stop the service and free resources stopSelf(); } public RemoteViews buildRemoteView(Context context) { RemoteView updateView = null; updateView = new RemoteViews(context.getPackageName(),R.layout.my_widget_layout); // Your code to build and update the remote view return updateView; } @Override public void onConfigurationChanged(Configuration newConfig) { int oldOrientation = this.getResources().getConfiguration().orientation; if(newConfig.orientation != oldOrientation) { // Update the widget RemoteView remoteView = buildRemoteView(this); // Push update to homescreen pushUpdate(remoteView); } } private void pushUpdate(RemoteView remoteView) { ComponentName myWidget = new ComponentName(this,MyWidget.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(myWidget,updateViews); } } }还有看看这个链接:http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html
另外,请务必指出您有兴趣收到清单中的轮换更改通知.这样的事情将会起作用:
机器人:configChanges = “keyboardHidden |方向”
在清单内的服务声明中声明.
希望有帮助!