Отмена привязки кнопок Android AppWidget - PullRequest
1 голос
/ 22 марта 2011

У меня есть виджет, который позволяет выбирать из 2 размеров, он также имеет конфигурацию.По какой-то причине, через некоторое время, кнопки на моем виджете отвяжутся, и вы не сможете ничего нажимать.Я не знаю, почему это происходит.Может ли это быть super.onReceive (context, intent) в моем методе OnRecieve?Может ли это привести к тому, что это может быть отменено?Кроме того, каким будет надежный способ убедиться, что кнопки ВСЕГДА связаны друг с другом?

AppWidgetProvider

public class mWidget extends AppWidgetProvider {
    public static String ACTION_WIDGET_REFRESH = "Refresh";
    public static final String PREFS_NAME = "mWidgetPrefs";

@Override
public void onEnabled(Context context) {

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

        RemoteViews remoteViews = buildLayout(context);

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);


}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
        int appWidgetId) {
    RemoteViews views = buildLayout(context);

    appWidgetManager.updateAppWidget(appWidgetId, views);
}

public static RemoteViews buildLayout(Context context) {


    RemoteViews remoteView = new RemoteViews(context.getPackageName(),
            R.layout.widget_4x2);
    Intent intent = new Intent(context, mWidget.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, 0);

    intent = new Intent(context, mWidget.class);
    intent.setAction(ACTION_WIDGET_REFRESH);
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    remoteView.setOnClickPendingIntent(R.id.refresh, pendingIntent);

    return remoteView;

}

@Override
public void onReceive(Context context, Intent intent) {



        RemoteViews remoteView = null;
        remoteView = new RemoteViews(context.getPackageName(),
                R.layout.widget_4x2);
        if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
Toast.makeText(context, "here", Toast.LENGTH_LONG).show();

        } else {
            super.onReceive(context, intent);
        }


}

} Манифест

<?xml version="1.0" encoding="utf-8"?>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".mwidgetConfig" android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>
    <activity android:name=".mwidgetConfigSmall"
        android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>
    <!-- BEGIN 4X4 WIDGET  -->
    <receiver android:label="Test Widget 4x4"
        android:name="com.test.mwidget.mWidget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action
                android:name="com.test.mwidget.mWidget.ACTION_WIDGET_REFRESH" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/widget_4x4_provider" />
    </receiver>
    <!-- BEGIN 4X2 WIDGET  -->
    <receiver android:label="Test Widget 4x2"
        android:name="com.test.mwidget.mWidgetSmall">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action
                android:name="com.test.mwidget.mWidgetSmall.ACTION_WIDGET_REFRESH" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/widget_4x2_provider" />
    </receiver>

</application>

Ответы [ 2 ]

2 голосов
/ 22 марта 2011
  1. Вы должны связать свое ожидаемое намерение с каждым onUpdate событием.
  2. Вы должны пройти через все appWidgerIds.

Обычно я реализую виджет следующим образом:

    public void onUpdate(Context context, 
                         AppWidgetManager appWidgetManager, 
                         int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        updateAllWidgetsInternal(context, appWidgetManager, appWidgetIds);
    }

    private static void updateAllWidgetsInternal(Context context, 
                                                 AppWidgetManager appWidgetManager,
                                                 int[] appWidgetIds) {
        final RemoteViews views = buildLayout(context);

        final int N = appWidgetIds.length;
            for (int i=0; i<N; ++i) {
            appWidgetManager.updateAppWidget(appWidgetIds[i], views);
        }
    }

    // This function can be executed anywhere in any time to update widgets
    public static void updateAllWidgets(Context context) {
        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, mWidget.class));
        updateAllWidgetsInternal(context, appWidgetManager, appWidgetIds);
}
0 голосов
/ 22 марта 2011

Я не понимаю, почему вы реализовали onReceive().

Это необходимо, только если ваш виджет настроен на перехват других трансляций, кроме стандартных трансляций виджетов, которые ACTION_APPWIDGET_DELETED, ACTION_APPWIDGET_DISABLED, ACTION_APPWIDGET_ENABLED и ACTION_APPWIDGET_UPDATE.

Если не требуется, попробуйте без onReceive().

...