【问题标题】:Widget's buttons doesn't work after phone restart手机重启后小部件按钮不起作用
【发布时间】:2012-07-12 10:21:54
【问题描述】:

嗯,我有一个桌面小部件,里面有几个按钮。每个按钮都应该启动一个活动。一切都很顺利,但是当我重新启动手机时,小部件按钮不再起作用。当然,当我删除小部件并再次添加它时,一切都会恢复正常。有什么问题?我是这样实现的:

清单

<receiver android:name=".MyWidget">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        ...
    </intent-filter>
    ...
</receiver>

接收器

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        // in service, due to possible ANR erors
        Intent newIntent = new Intent(context, UpdateService.class);
        newIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        context.startService(newIntent);
        return;
    }

UPDATESERVICE(其 INTENT SERVICE 不是标准 SERVICE)

    private boolean update() {
                    ...

            popup.showShort("whoaaa - onUpdate");

            i = new Intent(this, SettingsActivity.class);
            pi = PendingIntent.getActivity(context, 2, i, PendingIntent.FLAG_CANCEL_CURRENT);
            updateViews.setOnClickPendingIntent(R.id.btnSettings, pi);

            i = new Intent(this, AboutActivity.class);
            pi = PendingIntent.getActivity(context, 3, i, PendingIntent.FLAG_UPDATE_CURRENT);
            updateViews.setOnClickPendingIntent(R.id.btnAbout, pi);

                   ...
        }

最好的是,我可以在每次更新时看到弹出窗口,即使在手机重新启动后也是如此。所以它 100% 确定重启后我的receiver 运行它的onUpdate 方法,它触发我的UpdateService(它的意图服务)并且它的update 方法运行正确。那么,重新启动后,我的小部件中的任何按钮都不会对点击做出反应,这怎么可能呢?就像没有注册(或交付)意图一样。

PS:也许你会说,我需要为BOOT_COMPLETED 注册接收器,但我知道只有当你需要做一些特殊工作时才有必要(例如重新启动后重新注册警报 - 这不是我的情况) .

【问题讨论】:

  • 多年后出现同样的问题 xD

标签: android android-widget widget boot


【解决方案1】:

绝对看不懂,但是当我从onUpdate 方法(在接收器中)添加代码时:

Intent newIntent = new Intent(context, UpdateService.class);
newIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
context.startService(newIntent);

onEnabled 它可以工作...但它真的很难理解,因为我的更新方法(在 UpdateService 中)总是被正确触发,我看到了弹出...

现在它被触发了两次(从 onEnabled 触发,然后立即从 onUpdate 触发)并且它可以工作...请谁能解释一下?我可能错过了文档中非常重要的内容。

编辑:更正,它只在某些时候有效....奇怪

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多