android AppWidget按钮解除绑定

发布于 2024-10-24 23:28:34 字数 3898 浏览 3 评论 0原文

我有一个小部件,可以让您从 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>

I have a widget that lets you select from 2 sizes, it also has a config. For some reason after some time goes by, the buttons on my widget will unbind and you will not be able to click anything. I dont know why this is happening. Could it be the super.onReceive(context, intent) in my OnRecieve method? Would that cause it to unbind possibly? Also what would the sure fire way to make sure the buttons are ALWAYS binded be?

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


}

}
Manifest

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2024-10-31 23:28:34
  1. 您应该将待处理的 Intent 绑定到每个 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);
}
  1. You should bind your pending intent on each onUpdate event.
  2. You should iterate through all appWidgerIds.

Usually, I implement widget stuff as the following:

    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);
}
触ぅ动初心 2024-10-31 23:28:34

我不明白您为什么要实现 onReceive()

仅当您的小部件配置为捕获标准小部件广播之外的其他广播时才需要这样做,这些广播是 ACTION_APPWIDGET_DELETEDACTION_APPWIDGET_DISABLEDACTION_APPWIDGET_ENABLEDACTION_APPWIDGET_UPDATE

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

I don't understand why you implemented onReceive().

It's only necessary if your widget is configured to catch other broadcasts than the standard widgets broadcast which are ACTION_APPWIDGET_DELETED, ACTION_APPWIDGET_DISABLED, ACTION_APPWIDGET_ENABLED and ACTION_APPWIDGET_UPDATE.

If not necessary, try without onReceive().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文