android AppWidget按钮取消绑定

时间:2011-03-22 17:56:39

标签: android widget android-appwidget

我有一个小部件,可以让你从2种尺寸中选择,它也有一个配置。由于某种原因经过一段时间后,我的小部件上的按钮将解除绑定,您将无法单击任何内容。我不知道为什么会这样。可能是我的OnRecieve方法中的super.onReceive(context,intent)?这会导致它解开吗?另外,确保按钮总是绑定的确定的消防方式是什么?

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 个答案:

答案 0 :(得分:2)

  1. 您应该在每个onUpdate事件上绑定待处理的意图。
  2. 您应该遍历所有appWidgerIds
  3. 通常,我会将widget内容实现如下:

        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);
    }
    

答案 1 :(得分:0)

我不明白为什么你实施onReceive()

只有当您的窗口小部件配置为捕获除ACTION_APPWIDGET_DELETEDACTION_APPWIDGET_DISABLEDACTION_APPWIDGET_ENABLEDACTION_APPWIDGET_UPDATE的标准窗口小部件之外的其他广播时,才有必要。

如果没有必要,请尝试不使用onReceive()