【问题标题】:android widget update period milles not workingandroid小部件更新期千分尺不起作用
【发布时间】:2011-07-09 11:23:47
【问题描述】:

我是一名新的 android 开发人员,正在尝试编写小部件.. “updatePeriodMillis”参数告诉小部件在给定的持续时间之后调用 onupdate 方法,但对我来说并没有发生......

谁能指出我的错误..这是我的文件

src/hellowidget.java

package de.thesmile.android.widget;

import android.appwidget.AppWidgetProvider;
import java.util.Random;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;



public class HelloWidget extends AppWidgetProvider {
    int number =0;


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

         number = (new Random().nextInt(100));
        for (int appWidgetId : appWidgetIds) {


            RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.main);
            views.setTextViewText(R.id.TextView01, String.valueOf(number));
            appWidgetManager.updateAppWidget(appWidgetId, views);

        }

    }


}

xml/hello_widget_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/main"
/>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:orientation="vertical"

android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView><TextView android:padding="10dip" android:layout_marginTop="5dip" android:layout_gravity="center_horizontal|center" android:text="@string/widget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/widget_textview" android:textColor="@android:color/black"></TextView>


</LinearLayout>

安卓清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.thesmile.android.widget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".HelloWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/hello_widget_provider" />
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    小部件的更新时间被强制为至少 30 分钟,以避免编程不良的小部件导致电池干燥。使用AlarmManager 来解决这个问题(请参阅this post)。

    【讨论】:

      【解决方案2】:

      updatePeriodMillis 属性定义了 App Widget 框架通过调用 onUpdate() 回调方法向 AppWidgetProvider 请求更新的频率。使用此值不能保证实际更新准确及时,我们建议尽可能不频繁地更新 - 可能不超过一小时一次以节省电池电量。

      如果,您需要更频繁地更新(即在您的情况下为 1 秒,完全不推荐)。为此,请使用 AlarmManager 使用 AppWidgetProvider 接收的 Intent 设置警报。将警报类型设置为 ELAPSED_REALTIME 或 RTC,这将仅在设备唤醒时发出警报。然后将 updatePeriodMillis 设置为零(“0”)。

      【讨论】:

        猜你喜欢
        • 2012-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-27
        • 1970-01-01
        • 2015-11-14
        • 2012-01-06
        相关资源
        最近更新 更多