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

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

问题描述

我一直在努力解决这个问题了三天,现在我已经看了每一个问题就在这里的回答。我上有一个按钮,一个小部件,所有我想它做的是启动服务每次被点击。问题是,该按钮停止工作随机。在所有我能不能重新创建它,我不知道是什么原因造成的。我的服务电话stopSelf();但我有这个问题,一个广播接收器也,所以我认为是在小部件,而不是在服务的问题。这里是code

 公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds)
{

Log.i(小部件,的onupdate);

最终诠释N = appWidgetIds.length;

    的for(int i = 0; I&n种;我++)
    {

        INT appWidgetId = appWidgetIds [I]

        Log.i(小部件,onUpdateLoop);


        意向意图=新的意图(背景下,ServiceWidgetAction.class);
        intent.setAction(Long.toString(System.currentTimeMillis的()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent
                .getService(背景下,0,意向,PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews意见=新RemoteViews(context.getPackageName()
               R.layout.widget);

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

        views.setTextViewText(
                R.id.widgetTextView,
                一些文本);

        appWidgetManager.updateAppWidget(appWidgetId,意见);

    }

}
 

我检查这个code一遍又一遍,我不知道为什么它是这样做的。它这样做是在3个不同的手机,我不能重新创建它。我试过力停止的应用程序,从任务管理器清除它添加新部件等,以获得部件停止工作,也不会。但是今天早上当我醒来的小工具不再有效。任何帮助将不胜AP preciated。

解决方案

我最终找出问题

。我正在更新从其他地方的小部件的应用程序,我没有完整的code的每一个。换句话说我的应用程序更新textviews在这样的小部件

  RemoteViews意见=新RemoteViews(context.getPackageName()
           R.layout.widget);

    views.setTextViewText(
            R.id.widgetTextView,
            一些文本);

    appWidgetManager.updateAppWidget(appWidgetId,意见);
 

但它需要整个code保持注册这样的按钮:

 意向意图=新的意图(背景下,ServiceWidgetAction.class);
    intent.setAction(Long.toString(System.currentTimeMillis的()));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent
            .getService(背景下,0,意向,PendingIntent.FLAG_UPDATE_CURRENT);

    RemoteViews意见=新RemoteViews(context.getPackageName()
           R.layout.widget);

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

    views.setTextViewText(
            R.id.widgetTextView,
            一些文本);

    appWidgetManager.updateAppWidget(appWidgetId,意见);
 

每当你从你的应用程序中更新部件,确保它看起来酷似code在小部件本身那么底线是。不要留下任何东西了。

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

    }

}

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.

解决方案

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部件停止工作随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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