android AppWidget 按钮解绑

标签 android widget android-appwidget

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

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


}

} list

<?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>

最佳答案

  1. 您应该在每个 onUpdate 事件上绑定(bind)您的未决 Intent 。
  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);
}

关于android AppWidget 按钮解绑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395709/

相关文章:

java - 无法更新到新的 Android Material 版本

c++ - Qt - 什么是 QDockWidget::toggleViewAction 的常规小部件等价物

python - 使用交互式小部件运行 Bokeh

android - 应用程序小部件中的 ViewFlipper

android - 小部件中的 ListView 在滚动和调整大小时随机添加项目(嵌套远程 View )

android - 如何在android中实现缓存数据而不是从服务器加载数据

javascript - 无法捕获 Android Tv webview 应用程序上的键码(HTML+Javascript)

android - eclipse Juno 中的 google play services( Rev. 30) 中没有 libproject

java - Widget 中可点击的 ImageView?

android - 从 App Widget 使用 ShareCompat 库