本文介绍了Android appWidgetProvider onEnabled 从未在平板电脑上调用过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景 1:

  1. 设备已加载
  2. 我添加了第一个小部件
  3. OneX -> 调用 onEnabled |Galaxy Tab -> 不调用 onEnabled.
  1. Device is loaded
  2. I add first widget
  3. OneX -> Calls onEnabled | Galaxy Tab -> doesnt call onEnabled.

场景 2:

  1. 设备已启动,小部件已显示在屏幕上.
  2. OneX -> 调用 onEnabled |Galaxy Tab -> 启用时调用
  1. Device is booted with widget already on screen.
  2. OneX -> Calls onEnabled | Galaxy Tab -> Calls onEnabled

但是当用户在屏幕上放置一个时必须调用它(场景1),否则有什么意义.当然,您不必重新启动设备即可调用 onEnabled.

But It must be called when user places one on screen (scenario 1), else whats the point. Surely you dont have to restart your device to get onEnabled called.

这很重要,因为我附加了一个警报管理器以每 2 秒更新一次我的小部件:

Its important, because I Attach a alarm manager to update my widget every 2 seconds:

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 1);
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1000, createClockTickIntent(context));

有一个类似的问题没有有效的解决方案:AppWidgetProvider: not called onEnabled 方法

There is a similar question with no working solution: AppWidgetProvider: not called onEnabled method

没有必要注册这些意图,这样做也无济于事.

There is no need to register these intents, and doing so doesnt help anyway.

我的问题是为什么 onEnabled 没有调用,我该如何调用它.

My question is why is onEnabled not invoking, and how do I get it to.

谷歌文档上写着:ACTION_APPWIDGET_ENABLED:

当 AppWidget 的实例首次添加到主机时发送时间.如果存在 AppWidgetHost,则在启动时发送此广播安装了此提供程序的实例.

基本上确认它应该在两种情况下都可以工作

Basically confirming that it should work in both scenarios

这是触发onEnabled()

在响应 ACTION_APPWIDGET_ENABLED 广播时调用实例化此提供程序的 AppWidget.将此方法重写为实现您自己的 AppWidget 功能.

它可以在手机上运行,​​而不是在平板电脑上运行,这很奇怪.这是 android 3.2 的问题吗?有解决方法吗?

The fact it works on the phone, and not the tablet, is strange. Is it an issue with android 3.2 and is there a work around?

推荐答案

经过多次尝试找出它为什么会这样,我想不到.

After lots of attempt to find why it is acting this way, I came up short.

要么是 android 相关问题,要么是设备相关问题,其中启用了小部件的广播永远不会发送.

It is either an android related problem or a device related problem, where the widget enabled broadcast is never sent.

使用警报管理器更新小部件的唯一解决方法是从它自己的方法初始化警报管理器,然后从 onEnabledonUpdate 调用它.因此,当它没有达到 onEnabled 时,它将被 onUpdate 覆盖,此时小部件会自行更新(在应用程序调用它时).在场景 onEnabled 确实有效的情况下,它只会在调用 onUpdate 时用新实例替换现有的警报管理器.

The only work around to get the widget updating with alarm manager, was to initalise the alarm manager from its own method, and then call that from onEnabled AND onUpdate. So when it falls short of onEnabled, it will be covered by onUpdate, when the widget goes to update itself (on app call to it). And in the scenario onEnabled does work, it will just replace the existing alarm manager with a new instance when ever onUpdate is called.

根据您在 onEnabled 中尝试执行的操作,可能不是理想的解决方案,但我能找到的唯一解决方案.

Maybe not the ideal solution depending on what you are trying to do in onEnabled, but the only one I could find.

这篇关于Android appWidgetProvider onEnabled 从未在平板电脑上调用过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 00:57