启动器重新启动时小部件未更新

发布于 2024-11-19 14:05:12 字数 235 浏览 6 评论 0原文

我有一个小部件,每当配置发生更改(例如屏幕方向)以及手机解锁时,它就会自行更新。此过程涉及为我的小部件上的按钮设置 onClick 处理程序。这很有效,但是我发现有一个用例导致我的应用程序不响应 onClick 事件。这种特殊情况是每当启动器自行重新启动时。

有没有办法检测启动器何时重新启动,以便我可以手动更新我的小部件?或者是否有另一种方法可以确保 onClick 处理程序不会丢失?

I've got a widget which will update itself whenever there's a configuration change (such as screen orientation), and whenever the phone is unlocked. This process involves setting onClick handlers for the buttons on my widget. This works well, however I have found that there's a usage case which causes my app to not respond to onClick events. This particular case is whenever the launcher restarts itself.

Is there a way to detect when a launcher restarts, so I can update my widget manually? Or is there another way to ensure onClick handlers are not lost?

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

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

发布评论

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

评论(2

五里雾 2024-11-26 14:05:12

事实证明,我在发送 new RemoteViews() 垃圾邮件,而我本应该只调用它一次来生成视图,然后在需要时引用该实例。在我的解决方案中,我有一个类变量,用于存储这个单个 RemoteView 实例,以及一个用于访问它的 getter。

Turns out I was spamming new RemoteViews() when I should have just called it once to produce the view, and then referred to that one instance when required. In my solution, I have a class variable which stores this single RemoteView instance, and a getter to access it.

烂人 2024-11-26 14:05:12

@Glitch 的提议可能不适用于某些情况,尤其是带有 ListView 的应用程序小部件。这是因为多次调用 appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id) 后,ListView 会变得非常慢(尝试滚动 ListView) 。

我的猜测是,单个 RemoteView 实例会将其所有执行的指令保留在列表中。随着时间的推移,指令列表将会不断增长。每次appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id)时,大指令列表都会重新执行。

我建议的解决方案如下。但是,我相信它仅适用于某些设备,因为并非所有设备在启动器重新启动期间都会收到相同的广播消息。

@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals("com.sec.android.widgetapp.APPWIDGET_RESIZE")) {
        // http://stackoverflow.com/questions/17396045/how-to-catch-widget-size-changes-on-devices-where-onappwidgetoptionschanged-not
        handleTouchWiz(context, intent);

        // Possible launcher restart.
        handleLauncherRestart(context, intent);
    } else if (action.equals("android.appwidget.action.APPWIDGET_UPDATE_OPTIONS")) {

        // Possible launcher restart.            
        handleLauncherRestart(context, intent);
    } 

    super.onReceive(context, intent);
}

private void handleLauncherRestart(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
    updateAppWidget(context, appWidgetManager, appWidgetId);
}

private void handleTouchWiz(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    int appWidgetId = intent.getIntExtra("widgetId", 0);
    int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
    int widgetSpanY = intent.getIntExtra("widgetspany", 0);

    if (appWidgetId > 0 && widgetSpanX > 0 && widgetSpanY > 0) {
        Bundle newOptions = new Bundle();
        // We have to convert these numbers for future use
        // http://stackoverflow.com/questions/10008521/appwidget-size-calculation
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 74 - 2);
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 74 - 2);

        } else {
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 70 - 30);
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 70 - 30);
        }

        onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }
}

Proposal by @Glitch might not work for certain cases, especially app widget with ListView. This is because ListView will get very slow (Try to scroll the ListView) after appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id) had been called several time.

My guess is, the single RemoteView instance will keep all its executed instruction in a list. Over the time, the instruction list will grow. Every time appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, list_id), the large instruction list will be executed all over again.

My proposed solution is as follow. However, I believe it will only work on certain device, as not all devices will receive same broadcast message during launcher restarting.

@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals("com.sec.android.widgetapp.APPWIDGET_RESIZE")) {
        // http://stackoverflow.com/questions/17396045/how-to-catch-widget-size-changes-on-devices-where-onappwidgetoptionschanged-not
        handleTouchWiz(context, intent);

        // Possible launcher restart.
        handleLauncherRestart(context, intent);
    } else if (action.equals("android.appwidget.action.APPWIDGET_UPDATE_OPTIONS")) {

        // Possible launcher restart.            
        handleLauncherRestart(context, intent);
    } 

    super.onReceive(context, intent);
}

private void handleLauncherRestart(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
    updateAppWidget(context, appWidgetManager, appWidgetId);
}

private void handleTouchWiz(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    int appWidgetId = intent.getIntExtra("widgetId", 0);
    int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
    int widgetSpanY = intent.getIntExtra("widgetspany", 0);

    if (appWidgetId > 0 && widgetSpanX > 0 && widgetSpanY > 0) {
        Bundle newOptions = new Bundle();
        // We have to convert these numbers for future use
        // http://stackoverflow.com/questions/10008521/appwidget-size-calculation
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 74 - 2);
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 74 - 2);

        } else {
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 70 - 30);
            newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 70 - 30);
        }

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