我可以在片段中使用AppWidgetHost吗?我无法将大多数小部件添加到我的AppWidgetHost中进行更新.这是我的情景..
LauncherActivity.java
>非常简单,只需使用布局调用setContentView.
main.xml中
>包含一些片段,包括其中包含AppWidgetHost的片段.
CodeRedWidgetHostFragment.java
> onCreateView
创建AppWidgetHost&的实例AppWidgetManager
使用我存储在首选项中的窗口小部件ID创建和设置主机视图
> onStart()
appWidgetHost.startListening()
> onStop()
appWidgetHost.stopListening()
当我得到AppWidgetHost和AppWidgetManager的实例时,我正在使用getActivity()来获取Fragments托管活动.我想知道这是不是我的小部件没有更新的原因?
有些小部件实际上会更新,例如模拟时钟,但是,Youtube小部件不会自动循环显示视频缩略图.
我应该提一下,我在AppWidgetHost中选择了我想要的小部件,在另一个活动中将所选小部件的id存储在SharedPreferences中.
这是我的Fragment类的代码.
package com.brockoli.android.codered.widgethost; import android.app.Fragment; import android.appwidget.AppWidgetHost; import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProviderInfo; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.brockoli.android.codered.R; public class CodeRedWidgetHostFragment extends Fragment { private static final String TAG = CodeRedWidgetHostFragment.class.getSimpleName(); public static final String CODERED_WIDGET_ID = "CODERED_WIDGET_ID"; private SharedPreferences mSharedPrefsWidgets; AppWidgetManager mAppWidgetManager; AppWidgetHost mAppWidgetHost; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View parentView = inflater.inflate(R.layout.widget_host,null); mSharedPrefsWidgets = getActivity().getSharedPreferences("codered_widgets",0); mAppWidgetManager = AppWidgetManager.getInstance(getActivity()); mAppWidgetHost = new AppWidgetHost(getActivity(),R.id.APPWIDGET_HOST_ID); createWidget(parentView); return parentView; } @Override public void onStart() { super.onStart(); mAppWidgetHost.startListening(); } @Override public void onStop() { super.onStop(); mAppWidgetHost.stopListening(); } @Override public void onResume() { super.onResume(); createWidget(getView()); } /** * Creates the widget and adds to our view layout. */ public void createWidget(View v) { if (v != null) { // Remove any existing widgets from the widget host ((ViewGroup) v).removeAllViews(); int appWidgetId = mSharedPrefsWidgets.getInt(CODERED_WIDGET_ID,-1); if (appWidgetId > -1) { AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity(),appWidgetId,appWidgetInfo); hostView.setAppWidget(appWidgetId,appWidgetInfo); ((ViewGroup) v).addView(hostView); Log.i(TAG,"The widget size is: " + appWidgetInfo.minWidth + "*" + appWidgetInfo.minHeight); } } } }
解决方法
基于Clemens的回答,AppWidgetHost应该在应用程序的整个生命周期中保持活跃状态.这意味着无论Activity如何,您都应该可以轻松访问应用程序的AppWidgetHost和AppWidgetManager实例.
public class YourApp extends Application { private static final int HARDCODED_ID = 0; private static AppWidgetHost appWidgetHost; private static AppWidgetManager appWidgetManager; @Override public void onCreate() { super.onCreate(); appWidgetManager = AppWidgetManager.getInstance(getApplicationContext()); appWidgetHost = new AppWidgetHost(getApplicationContext(),HARDCODED_ID); appWidgetHost.startListening(); } @Override public void onTerminate() { super.onTerminate(); appWidgetHost.stopListening(); appWidgetHost = null; } public static AppWidgetHost getAppWidgetHost() { return appWidgetHost; } public static AppWidgetManager getAppWidgetManager() { return appWidgetManager; } }
我当前的实现已正确更新.测试了许多需要“实时”更新的应用,例如我的音乐播放器小部件.
请记住,应用程序上下文用于确保AppWidgetHost和AppWidgetManager的上下文始终可用.
此外,在其小部件中使用AppCompat库的较新小部件目前在com.android.support:appcompat-v7:23.1.1依赖项中存在错误.为避免这种情况,请在调用以下内容时使用应用程
AppWidgetHostView hostView = YourApp.getAppWidgetHost() .createView(context.getApplicationContexxt(),info);