2017-06-01 116 views
3

我的应用程序小部件永远不会去onUpdate函数 我添加3000毫米的测试,但除了第一次添加它, 它永远不会去onUpdate函数。appwidget永远不会去onUpdate

我在做什么错?

我的清单文件:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

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

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

我clockWidget:

/** 
* Implementation of App Widget functionality. 
*/ 
public class ClockWidgetClass extends AppWidgetProvider { 

    static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, 
           final int appWidgetId) { 

     //getting shered prefrences 
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
     String name = preferences.getString("randomString", ""); 
     if(!name.equalsIgnoreCase("")) 
     { 
      name = name; /* Edit the value here*/ 
     } 

     // Construct the RemoteViews object 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget_class); 

     //set chosen design visible 
     //set all visibility to 0 
     views.setViewVisibility(R.id.AnalogClock0, View.INVISIBLE); 
     views.setViewVisibility(R.id.AnalogClock1, View.INVISIBLE); 
     views.setViewVisibility(R.id.AnalogClock2, View.INVISIBLE); 
     views.setViewVisibility(R.id.AnalogClock3, View.INVISIBLE); 

     //turning on the correct clock 
     if (name.equals("1")) { 
      views.setViewVisibility(R.id.AnalogClock0, View.VISIBLE); 
     } else if (name.equals("2")) { 
      views.setViewVisibility(R.id.AnalogClock1, View.VISIBLE); 
     } else if (name.equals("3")) { 
      views.setViewVisibility(R.id.AnalogClock2, View.VISIBLE); 
     } else { 
      views.setViewVisibility(R.id.AnalogClock3, View.VISIBLE); 
     } 

//  views.setTextViewText(R.id.appwidget_text, name); 

     //updateting the widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 

    } 


    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     // There may be multiple widgets active, so update all of them 
     for (int appWidgetId : appWidgetIds) { 
      updateAppWidget(context, appWidgetManager, appWidgetId); 
     } 
    } 

    @Override 
    public void onEnabled(Context context) { 
     // Enter relevant functionality for when the first widget is created 
    } 

    @Override 
    public void onDisabled(Context context) { 
     // Enter relevant functionality for when the last widget is disabled 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Chain up to the super class so the onEnabled, etc callbacks get dispatched 
     super.onReceive(context, intent); 
     // Handle a different Intent 
     Log.d("reciving", "onReceive()" + intent.getAction()); 

    } 


} 

我的窗口小部件的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialKeyguardLayout="@layout/clock_widget_class" 
    android:initialLayout="@layout/clock_widget_class" 
    android:minHeight="110dp" 
    android:minWidth="250dp" 
    android:previewImage="@drawable/example_appwidget_preview" 
    android:resizeMode="horizontal|vertical" 
    android:updatePeriodMillis="3000" 
    android:widgetCategory="home_screen"></appwidget-provider> 

回答

2

决不可能不正确。它只是不会发生在你期望它发生的时间。随着documentation for updatePeriodMillis你会发现一个重要的注意事项:

注意:更新与updatePeriodMillis请求将不会发送超过每30分钟一次。

因此,您可能需要等待30分钟才能再次致电onUpdate()