Android 小部件按钮停止工作 [英] Android widget buttons stop working

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

问题描述

我有一个带有小部件的 Android 应用程序,它有按钮.此代码有效.

I have an Android application with a widget, that has buttons. This code works.

当发生某些事情时,小部件上的按钮停止工作,例如更改手机的语言.我使用共享首选项,所以如果用户重新安装应用程序(没有卸载),按钮将再次起作用并且设置保持不变.

The buttons on the widget stop working when something happens, such as changing the language of the phone. I use shared preferences, so if the user reinstalls the app (without uninstalling), the buttons are working again and the settings remain the set ones.

  • 我注意到我的 AppWidgetProvider 类(此分析下的代码)中的 Intent 未正确触发.
  • 我向 AppWidgetProvider 实例化的 Call1 类添加了 Toast 消息,但它没有显示.
  • 我的 UpdateService.java 只是获取设置的首选项并自定义小部件的外观,所以我认为这可能与我的问题无关.
  • 我的 Main.java 文件仅包含微调器并保存共享首选项,这意味着我在微调器中选择计算机",以便计算机"文本出现在小部件上.当我更改手机的语言时,它也不会消失,图像也不会消失.所以,我相信UpdateService.java一定是可以的.
  • I have noticed the Intents in my AppWidgetProvider class (code beneath this analysis) are not fired appropriately.
  • I added a Toast message to the Call1 class instantiated from AppWidgetProvider, but it doesn't display.
  • My UpdateService.java is just getting the set preferences and customizing the widget's appearance, so I don't think it could possibly be related to my issue.
  • My Main.java file merely consists of spinners and saves shared preferences, which means I select "Computer" in a spinner, so that the "Computer" text appears on the widget. It also does not disappear when I change the language of the phone, and neither do images. Therefore, I believe UpdateService.java must be ok.

这里是 AppWidgetProvider 类:

Here is the AppWidgetProvider class:

public class HelloWidget extends AppWidgetProvider {

    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_CONFIGURE2 = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
    public static String ACTION_WIDGET_RECEIVER2 = "ActionReceiverWidget";
    private static final int REQUEST_CODE_FOUR = 40;
    private static final int REQUEST_CODE_FIVE = 50;
    private static final int REQUEST_CODE_SIX = 60;
    private static final int REQUEST_CODE_SEVEN = 70;
    private static final int REQUEST_CODE_EIGHT = 80;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        context.startService(new Intent(context, UpdateService.class));
        //Intent widgetUpdateIntent = new Intent(context, UpdateService.class);
        //context.startService(widgetUpdateIntent );

         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain2);

         //P1 starts Call1.class
         Intent configIntent4 = new Intent(context, Call1.class);
         configIntent4.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent4 = PendingIntent.getActivity(context, REQUEST_CODE_FOUR, configIntent4, 0);
         remoteViews.setOnClickPendingIntent(R.id.ImageView01, configPendingIntent4);

         //P2 starts Call2.class
         Intent configIntent5 = new Intent(context, Call2.class);
         configIntent5.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent5 = PendingIntent.getActivity(context, REQUEST_CODE_FIVE, configIntent5, 0);
         remoteViews.setOnClickPendingIntent(R.id.ImageView02, configPendingIntent5);

         //P3 starts Call3.class
         Intent configIntent6 = new Intent(context, Call3.class);
         configIntent6.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent6 = PendingIntent.getActivity(context, REQUEST_CODE_SIX, configIntent6, 0);
         remoteViews.setOnClickPendingIntent(R.id.ImageView03, configPendingIntent6);

         //P4 starts Call4.class
         Intent configIntent7 = new Intent(context, Call4.class);
         configIntent7.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent7 = PendingIntent.getActivity(context, REQUEST_CODE_SEVEN, configIntent7, 0);
         remoteViews.setOnClickPendingIntent(R.id.ImageView04, configPendingIntent7);

         //P5 starts Call5.class
         Intent configIntent8 = new Intent(context, Call5.class);
         configIntent8.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent8 = PendingIntent.getActivity(context, REQUEST_CODE_EIGHT, configIntent8, 0);
         remoteViews.setOnClickPendingIntent(R.id.ImageView05, configPendingIntent8);

         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
   }

    @Override
    public void onReceive(Context context, Intent intent) {

        final String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) 
        {
            final int appWidgetId = intent.getExtras().getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) 
            {
                this.onDeleted(context, new int[] { appWidgetId });
            }
        } 
        else 
        {
            if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) 
            {
                String msg = "null";
                try {
                    msg = intent.getStringExtra("msg");
                    } catch (NullPointerException e) {
                    //Log.e("Error", "msg = null");
                    }
            }
            super.onReceive(context, intent);
            }
    }
}

我还有一个 EditPreferences.javaGlobalVars.java 和其他一些现在没有意义的类.类的名称不言自明.

I also have an EditPreferences.java, GlobalVars.java and some other now meaningless classes. The names of the classes speak for themselves.

另一件事.我也有一个 Widgetmain.java:

One other thing. I also have a Widgetmain.java:

  public class WidgetMain extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widgetmain2);
    }
    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) 
    {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain2);

          appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

    }
}

<小时>

这个怎么样:

当我在同事的 ZTE Blade 上安装此应用程序时,小部件上的文本视图未加载适当的文本,仅加载了 strings.xml 中确定的文本.

When I install this app on my colleague's ZTE Blade the textviews on the widget are not loaded with the appropriate text, just with the one determined in the strings.xml.

当我重新安装应用程序(不卸载)时,文本视图已加载并且一切正常.这个问题在我的 HTC Desire HD 上没有出现.

When I reinstall the app (without uninstalling), the textviews are loaded and everything is fine. This problem doesn't emerge on my HTC Desire HD.

文本视图像这样(部分代码)加载到上述 UpdateService.java 中:

The textviews are load in the aforementioned UpdateService.java like this (part of the code):

RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.main);
updateViews.setTextViewText(R.id.widget_textview, name);
ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);

即使名称"是静态的(例如 String name="Something"),第一次安装时仍不会加载该文本视图.

Even if "name" is static (e.g. String name="Something"), that textview is still not loaded at the first install.

推荐答案

我的 UpdateService.java 只是获取设置的首选项并自定义小部件的外观,所以我认为这可能与我的问题无关.

My UpdateService.java is just getting the set preferences and customizing the widget's appearance, so I don't think it could possibly be related to my issue.

它可能是相关的,因为您可以使用它来刷新"待处理的意图.我的 appwidget 中有一个类似的问题,即图像按钮在随机运行时间(几小时)后停止响应点击.

It is possible it is related, in as much that you could use it to "refresh" the pending intent. I have a similar issue in my appwidget that an image button stops responding to clicks after some random run time (hours).

我找到了这个帖子:AppWidget 按钮 onClick 停止工作

还有这句话:

待处理的意图在每次使用后被烧毁".您需要重新设置它.或者等待小部件刷新,然后它也会发生,但这可能不是所需的方式.

The pending intent is "burned" after each use. You need to set it again. Or wait for the widget to get refreshed, then it happens, too, but that's probably not the desired way.

鉴于小部件更新时间通常设置为数小时或数天(我的是 86400000 毫秒),以防止手机每隔几分钟就退出暂停状态,因此您的小部件不会经常运行 onUpdate.在更新服务中设置挂起的 Intent ALSO 可能会阻止您描述的问题.每次更新服务运行时,都会重新创建挂起的 Intent.

Given that the widget update time normally is set at many hours or days (mine is 86400000 milli seconds) in order to prevent the phone going out of suspend every so many minutes your widget will not often run onUpdate. It is possible that setting the pending intent ALSO in the update service will prevent the problem you describe.Each time the update service runs the pending intent is re-created.

我今天已将这个可能的修复添加到我的 appwidget,我必须等待,看看修复是否真的有效,但到目前为止一切都很好.

I have today added this possible fix to my appwidget and I have to wait and see if the fix really works, but so far so good.

我在刷新每个小部件的更新服务循环中添加了以下代码:

I added the following code in the update service' loop where it refreshes each widget:

for (int i=0; i<appWidgetIds.length; i++)
{
  appWidgetId=appWidgetIds[i];

  /* other stuff to do */

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

  /* here you "refresh" the pending intent for the button */
  Intent clickintent=new Intent("net.example.appwidget.ACTION_WIDGET_CLICK");
  PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
  views.setOnClickPendingIntent(R.id.example_appwidget_button, pendingIntentClick);
  appWidgetManager.updateAppWidget(appWidgetId, views);

  /* then tell the widget manager to update */
  appWidgetManager.updateAppWidget(appWidgetId, views);
}

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

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