简体   繁体   English

将数据从Android应用程序获取到小部件并显示

[英]Getting data from Android app to widget and displaying it

I have started implementing a widget into an application of mine and I am having some trouble getting the data from the application to the widget. 我已经开始在我的应用程序中实现小部件,但是在将数据从应用程序传递到小部件时遇到了一些麻烦。

Here is the code for my widget: 这是我的小部件的代码:

public class ElectricityMonitorWidgetProvider extends AppWidgetProvider {

    public static final String WIDGETTAG = "WidgetMood";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        System.out.println("Widget onUpdate called..");

        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            System.out.println("In loop. App widget id: "+appWidgetId);
        }
    }    
}

In my widget I have only a textview that I want updated with a number from my application. 在我的小部件中,只有一个文本视图,我想用我的应用程序中的数字进行更新。 The application gets this number via a web request. 应用程序通过Web请求获取此号码。

In the onUpdate() method which runs periodically I want to be able to update that one textview. 在定期运行的onUpdate()方法中,我希望能够更新一个textview。

I am not sure how to achieve this. 我不确定如何实现这一目标。

Do I need to implement a helper class that the widget calls and inside that helper class I have the same method as the application uses to receive the data it uses or? 我是否需要实现一个由小部件调用的帮助程序类,并且在该帮助程序类中是否具有与应用程序用于接收其使用的数据的方法相同的方法,或者?

This is my first time implementing a widget. 这是我第一次实现小部件。 I have read through the guide on widgets on developer.android.com and that helped me get the widget to show on my phone. 我已经阅读了developer.android.com上有关小部件的指南,这有助于我将其显示在手机上。 The only thing I am missing is the functionality that I describe. 我唯一缺少的是我描述的功能。

Thanks for any help. 谢谢你的帮助。

put this in your widget updating loop 把它放在您的小部件更新循环中

for (int i=0; i<N; i++) {
  int appWidgetId = appWidgetIds[i];
  int value = i; //replace with actual value eg from SharedPreferences
  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.your_widget);
  remoteViews.setTextViewText(R.id.text_view, String.valueOf(value)); //assumed that R.layout.your_widget contains a TextView with R.id_text_view id

  appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
  System.out.println("In loop. App widget id: "+appWidgetId);
}

Please note, that this code does not specify any action upon user touching the widget (generally you need to specify a PendingIntent and hook it with RemoteViews.setOnClickPendingIntent method). 请注意,此代码不会在用户触摸窗口小部件时指定任何操作(通常需要指定一个PendingIntent并使用RemoteViews.setOnClickPendingIntent方法对其进行挂钩)。

Also, I would suggest that you update your widget when needed and not periodically eg your app could send custom Intent to your AppWidgetProvider, which would handle it in overridden onReceive() and update RemoteViews on widgets. 另外,我建议您在需要时而不是定期更新窗口小部件,例如,您的应用程序可以向AppWidgetProvider发送自定义的Intent,后者将在覆盖的onReceive()处理它,并在窗口小部件上更新RemoteViews。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM