Android Widget 随机停止工作 [英] Android Widget stops working randomly

查看:33
本文介绍了Android Widget 随机停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决这个问题三天了,我已经查看了这里的每一个问题以寻求答案.我有一个带有按钮的小部件,我想做的就是每次单击它时启动一个服务.问题是按钮随机停止工作.我根本无法重新创建它,我不知道是什么原因造成的.我的服务调用 stopSelf();但是我的广播接收器也遇到了这个问题,所以我相信问题出在小部件上,而不是服务上.这是代码

I have been working with this problem for three days now and I've looked at every single question on here for an answer. I have a widget with a button on it and all I would like it to do is start a service everytime it is clicked. The problem is that the button stops working randomly. I can't recreate it at all and I have no idea what causes it. My service calls stopSelf(); but I have had this problem with a broadcast receiver also so I believe the problem to be in the widget and not in the service. Here is the code

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]     appWidgetIds) 
{

Log.i("Widget", "onUpdate");

final int N = appWidgetIds.length;

    for (int i=0; i<N; i++) 
    {

        int appWidgetId = appWidgetIds[i];

        Log.i("Widget", "onUpdateLoop");


        Intent intent = new Intent(context, ServiceWidgetAction.class);
        intent.setAction(Long.toString(System.currentTimeMillis()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent
                .getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), 
               R.layout.widget);

        views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);

        views.setTextViewText(
                R.id.widgetTextView,
                "Some text");

        appWidgetManager.updateAppWidget(appWidgetId, views);

    }

}

我一遍又一遍地检查了这段代码,但我不知道它为什么会这样.它在 3 部不同的手机上执行此操作,我无法重新创建它.我试过强制停止应用程序,从任务管理器中清除它,添加新的小部件等,以使小部件停止工作,但它不会.但是,当我今天早上醒来时,小部件不再起作用.任何帮助将不胜感激.

I've checked this code over and over and I have no idea why it is doing this. It does this on 3 separate phones and I can't recreate it. I've tried force stopping the app, clearing it from a task manager adding new widgets etc. to get the widget to stop working and it won't. However when I woke up this morning the widget no longer works. Any help would be GREATLY appreciated.

推荐答案

我最终找出了问题所在.我正在从应用程序的其他地方更新小部件,但我没有每个地方的完整代码.换句话说,我的应用程序像这样更新了小部件中的 textviews

I ended up figuring out the problem. I was updating the widget from other places in the app and I did not have the complete code for each one. In other words my app updated the textviews in the widget like this

    RemoteViews views = new RemoteViews(context.getPackageName(), 
           R.layout.widget);

    views.setTextViewText(
            R.id.widgetTextView,
            "Some text");

    appWidgetManager.updateAppWidget(appWidgetId, views);

但它需要整个代码来保持按钮注册如下:

But it needed the entire code to keep the button registered like this:

    Intent intent = new Intent(context, ServiceWidgetAction.class);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent
            .getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    RemoteViews views = new RemoteViews(context.getPackageName(), 
           R.layout.widget);

    views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);

    views.setTextViewText(
            R.id.widgetTextView,
            "Some text");

    appWidgetManager.updateAppWidget(appWidgetId, views);

因此,最重要的是,每当您从应用程序中更新小部件时,请确保它看起来与小部件本身中的代码完全相同.不要遗漏任何东西.

So bottom line is whenever you update a widget from within your app, make sure it looks exactly like the code in the widget itself. Don't leave anything out.

这篇关于Android Widget 随机停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆