如果调整大小,Android小部件将不会更新

时间:2017-08-07 06:39:43

标签: android

如果我改变它的大小,我的小部件不会更新。 我在这做错了什么?似乎onAppWidgetOptionsChanged无法正常工作。 如果我在窗口小部件提供程序中更新宽度和高度,它将不会更新。我每次都得到相同的宽度和高度。 这是我的课程代码:

public class wimWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    Toast.makeText(context, "onUpdate ", Toast.LENGTH_LONG).show();

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget);

     Intent intent = new Intent(context.getApplicationContext(),
            UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    Log.v("wim", "onUpdate-" + intent);

    // To react to a click we have to use a pending intent as the
    // onClickListener is
    // excecuted by the homescreen application
    PendingIntent pendingIntent = PendingIntent.getService(
            context.getApplicationContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    //remoteViews.setOnClickPendingIntent(R.id.imagewidget, pendingIntent);
    remoteViews.setOnClickPendingIntent(R.id.widget116, pendingIntent);
    // Finally update all widgets with the information about the click
    // listener
    ComponentName watchWidget;
    watchWidget = new ComponentName( context, wimWidget.class );

    //  appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    appWidgetManager.updateAppWidget(watchWidget, remoteViews);

    final int N = appWidgetIds.length;
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];
        Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);

        if(options!=null){
            //int nwidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
            onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, options);
        }
    }

    // Update the widgets via the service
    context.startService(intent);
}

@Override
public void onAppWidgetOptionsChanged (Context context, AppWidgetManager
        appWidgetManager, int appWidgetId, Bundle newOptions) {

    super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget);

    int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
    Toast.makeText(context, "Oppdatert - onAppWidgetOptionsChanged - minWidth: " + minWidth, Toast.LENGTH_LONG).show();


    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

}

private void updateWidget(Context context, AppWidgetManager appWidgetManager,

                          int appWidgetId, Bundle options) {
    Toast.makeText(context, "updateWidget", Toast.LENGTH_SHORT).show();
    // Do whatever you need to do to the widget. Include any customization based
    // on the size, included in the options. Be sure to have a graceful fallback if
    // no valid size data is included in the bundle.


}



 @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        //Toast.makeText(context, "onDeleted", Toast.LENGTH_SHORT).show();

     Intent serviceIntent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
     context.stopService(serviceIntent);

        super.onDeleted(context, appWidgetIds);
    }
}

这是提供者:

 <?xml version="1.0" encoding="utf-8"?>
 <appwidget-provider 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="50.0dp"
    android:minHeight="110.0dp"
   android:updatePeriodMillis="111600"
   android:initialLayout="@layout/widget"
 />

0 个答案:

没有答案