简体   繁体   English

我的android主屏幕小部件出现了,但没有更新

[英]My android homescreen widget appears, but is not updating

I already found others questions similar to mine, but could not find the solution. 我已经找到了其他与我类似的问题,但是找不到解决方案。 I have a custom widget with simple TextView and also a configuration Activity . 我有一个带有简单TextView和配置Activity的自定义窗口小部件。 Problem is: 问题是:

  1. When widget is added to homescreen, configurations activity appears, but onUpdate() method of AppWidgetProvider runs immediately, as soon as configuration activity is created. 将小部件添加到主屏幕后,将显示配置活动,但是一旦创建了配置活动, AppWidgetProvider onUpdate()方法将立即运行。 It should run after configuration activity is closed by pressing OK button. 它应在按OK按钮关闭配置活动后运行。

  2. Even if onUpdate() method run on configuration activity start (according to Toast in the method,showing proper data for textview), the TextView remains still not updated. 即使onUpdate()方法在配置活动开始时运行(根据该方法中的Toast,显示textview的正确数据),TextView仍然不会更新。

  3. Even if android:updatePeriodMillis is set to 1000ms, onUpdate method run only once - when configuration activity starts. 即使android:updatePeriodMillis设置为1000ms,onUpdate方法也只能运行一次-配置活动开始时。

  4. When I add the widget to my homescreen and then restart phone, the widget's TextView is finally updated with proper time, but only once. 当我将小部件添加到主屏幕然后重新启动手机时,小部件的TextView最终会在适当的时间更新,但只有一次。

Many users have been reporting similar problems on recent android versions, like 4.4. 许多用户在最近的Android版本(例如4.4)上报告了类似的问题。 Is there any final solution for this bad behavior? 对于这种不良行为,是否有任何最终解决方案?

Here are my files - Manifest: 这是我的文件-清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="incredible.riskofrain.app">

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/main_icon">

    <receiver android:name=".RORAppWidgetProvider" >

        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/ror_appwidget_info" />

    </receiver>

    <activity android:name=".RORAppWidgetConfigure">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
        </intent-filter>
    </activity>

    <activity android:name="incredible.riskofrain.app.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

appwidget_info: appwidget_info:

android:minWidth="200dp"
android:minHeight="80dp"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/ror_appwidget"
android:configure="incredible.riskofrain.app.RORAppWidgetConfigure"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"   >

AppWidgetProvider: AppWidgetProvider:

public class RORAppWidgetProvider extends AppWidgetProvider {

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

        final int N = appWidgetIds.length;
        Calendar c = Calendar.getInstance();
        String time = "" + c.get(Calendar.MINUTE)+ ":" + c.get(Calendar.SECOND);


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

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.ror_appwidget);
            views.setTextViewText(R.id.textview, time);

            appWidgetManager.updateAppWidget(appWidgetId, views);
            Toast.makeText(context, "onUpdate " + time + " " + appWidgetId, Toast.LENGTH_LONG).show();

        }

    }}

AppWidgetConfigure: AppWidgetConfigure:

public class RORAppWidgetConfigure extends Activity {

int appWidgetId; Button button; AppWidgetManager appWidgetManager;
RemoteViews views;

@Override
protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.ror_configure);
    button = (Button) findViewById(R.id.button);

    appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        appWidgetId = extras.getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            views = new RemoteViews(getPackageName(),  R.layout.ror_appwidget);
            appWidgetManager.updateAppWidget(appWidgetId, views);

            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_OK, resultValue);

            Toast.makeText(getApplication().getApplicationContext(), "Configure, ID: - " + appWidgetId+"", Toast.LENGTH_LONG).show();

            finish();

        }
    });

    super.onCreate(savedInstanceState);
}}

you can update Widget using broasdcast Receiver .. 您可以使用broasdcast Receiver更新小部件。

check this link: 检查此链接:

Reference Link 参考链接

<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".WordWidget" android:label="@string/widget_name">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_word" />
</receiver>

i hope its useful to you.. 我希望它对您有用。

I found this in documentation, probably this is the problem: 我在文档中找到了这个,可能是这个问题:

public int updatePeriodMillis Added in API level 3 How often, in milliseconds, that this AppWidget wants to be updated. public int updatePeriodMillis在API级别3中添加了此AppWidget想要更新的频率(以毫秒为单位)。 The AppWidget manager may place a limit on how often a AppWidget is updated. AppWidget管理器可能会限制AppWidget更新的频率。 This field corresponds to the android:updatePeriodMillis attribute in the AppWidget meta-data file. 此字段对应于AppWidget元数据文件中的android:updatePeriodMillis属性。 Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes. 注意:使用updatePeriodMillis请求的更新不会每30分钟交付一次。


I also found that there is a huge bug in android framework making configuration activity useless: https://code.google.com/p/android/issues/detail?id=3696 我还发现android框架中存在一个巨大的错误,使得配置活动无效: https : //code.google.com/p/android/issues/detail?id=3696

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

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