没有调用Onupdate,多次调用onreceive

时间:2014-01-10 12:26:01

标签: java android widget

我搜索过SO,但我没有遇到过同样的问题。

我正在开发一个配置活动的小部件,用户可以在其中设置颜色,文本大小和选择类别。基本思路是,当用户关闭配置活动时,将选择类别的数组发送到Onreceive方法,在该方法中我调用另一种方法,在该方法中我从XML文件中读取,过滤类别并填充另一个数组以便在Onupdate方法中读取。

Onreceived被调用,它填充并过滤我的内容,但我的问题是:

  1. 当我关闭配置活动onreceive时会被调用两次。
  2. 根本没有调用Onupdate方法。
  3. 我几乎尝试了所有事情,但我终于没有想法了,所以请帮助我。

    我的代码:

    Android Manifest:

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <receiver android:name=".WidgetProvider">
                <intent-filter>
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                </intent-filter>
                <meta-data android:name="android.appwidget.provider"
                    android:resource="@xml/widget_info" />
            </receiver>
            <activity android:name=".Nastavitve" android:label="@string/app_name" android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
                </intent-filter>
            </activity>
        </application>
    

    WidgetProvider xml

    <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
        android:updatePeriodMillis="1800000"
        android:minWidth="250dp"
        android:minHeight="50dp"
        android:initialLayout="@layout/widget_layout" android:configure="com.example.mislicitati.Nastavitve">
    </appwidget-provider>
    

    WidgetProvider.java

    public class WidgetProvider extends AppWidgetProvider {
    
        ArrayList<String> recepti = new ArrayList<String>();
        NodeList listCitat;
        static final ArrayList<HashMap<String, String>> list = new    ArrayList<HashMap<String, String>>();
    
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                int[] appWidgetIds) {
            // TODO Auto-generated method stub
            //random object from array
            Random rand = new Random();
    
            String besedilo = null, avtor = null;
    
            HashMap<String, String> map = new HashMap<String, String>();
            map = list.get(rand.nextInt(list.size()));
            for (Entry<String, String> mapEntry : map.entrySet()) {
                String key = mapEntry.getKey();
                String value = mapEntry.getValue();
    
                if (key == "avtor") {
                    avtor = value;
                }
    
                if (key == "besedilo") {
                    besedilo = value;
                }
            }
    
            for (int widgetId : appWidgetIds) {
                RemoteViews views = new RemoteViews(context.getPackageName(),
                        R.layout.widget_layout);
                views.setTextViewText(R.id.textView1, besedilo);
                views.setTextViewText(R.id.textView2, avtor);
                appWidgetManager.updateAppWidget(widgetId, views);
            }
            super.onUpdate(context, appWidgetManager, appWidgetIds);
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.e("onReceive", "poklican");
            ArrayList<String> izbranekat;
            izbranekat = intent.getStringArrayListExtra("kategorije");
            if(list.isEmpty())
            {
    
            napolniSeznam(context, izbranekat);
            }
            else
            {
                list.clear();
                napolniSeznam(context, izbranekat);
            }
            super.onReceive(context, intent);
    
        }
    

    Nastavitve.java //配置活动,只有onclick方法,我将数据发送到小部件

    public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d("mainOk", "called");
            // Configuration...
            // Call onUpdate for the first time.
            Log.d("Ok Button", "First onUpdate broadcast sending...");
            final Context context = Nastavitve.this;
            AppWidgetManager appWidgetManager = AppWidgetManager
                    .getInstance(context);
            ComponentName thisAppWidget = new ComponentName(
                    context.getPackageName(), Nastavitve.class.getName());
            // N.B.: we want to launch this intent to our AppWidgetProvider!
            Intent firstUpdate = new Intent(context, WidgetProvider.class);
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
    
            firstUpdate.putStringArrayListExtra("kategorije", izbkat);
            firstUpdate.setAction("android.appwidget.action.APPWIDGET_UPDATE");
            firstUpdate
                    .putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
            context.sendBroadcast(firstUpdate);
            Log.d("Ok Button", "First onUpdate broadcast sent");
            // Return the original widget ID, found in onCreate().
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
            setResult(RESULT_OK, resultValue);
            finish();
    

0 个答案:

没有答案