窗口小部件配置活动未在重新配置时更新窗口小部件。只有当它第一次被召唤时

时间:2013-09-04 20:22:55

标签: android android-widget widget sharedpreferences

我是Android的编程新手。我有一个可点击的文本小部件,配置活动只在第一次正常工作,即首次创建小部件时。

我希望能够单击窗口小部件,打开相同的配置活动,更改设置并使用新配置更新窗口小部件。这是我无法完成的事情。我也在使用SharedPreferences,但我仍然无法完成它。

我可能已经在这里阅读了所有相关的问题,也可以从其他来源阅读,但是nada。

请查看下面的代码,如果可以,请帮助我。我很感激。

配置类

package com.example.widget;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Spinner;

public class Config extends Activity {

private Config context;
private int widgetId ;
AppWidgetManager manager;
RemoteViews views;
Spinner lang_spinner;
String lang;
Button apply;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.config);
    setResult(RESULT_CANCELED);

    // Set some fields
    context = this;
    manager = AppWidgetManager.getInstance(context);
    views = new RemoteViews(context.getPackageName(), R.layout.main);

    // Start the preferences object
    SharedPreferences appPreferences = context.getSharedPreferences("WIDGET_PREFERENCES", 0);
    final SharedPreferences.Editor appEditor = appPreferences.edit();

    // Get the widget ID
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, 
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    // Functionality to apply language
    lang_spinner = (Spinner) findViewById(R.id.spinnerLang);
    lang_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            lang = lang_spinner.getSelectedItem().toString();
            if (lang.equals("Espanol")){
                Main.WIDGET_TEXT = getResources().getString(R.string.widget_text_ES);
                appEditor.putString("WT"+widgetId, getResources().getString(R.string.widget_text_ES)).commit();
            } else if (lang.equals("English")) {
                Main.WIDGET_TEXT = getResources().getString(R.string.widget_text_EN);
                appEditor.putString("WT"+widgetId, getResources().getString(R.string.widget_text_EN)).commit();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            appEditor.putString("WT"+widgetId, getResources().getString(R.string.widget_text_EN)).commit();
        }
    });


    // Apply settings
    apply = (Button) findViewById(R.id.apply);
    apply.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // Send a pending intent to the textview, to be able to click back to reconfigure
            Intent intent = new Intent(context, Config.class);
            PendingIntent pIConfig = PendingIntent.getActivity(context, 0, intent, 0);
            views.setOnClickPendingIntent(R.id.widgetText, pIConfig);

            // Set the text
            views.setTextViewText(R.id.widgetText, Main.WIDGET_TEXT);

            // Commit changes to the preferences object
            // appEditor.commit();

            // Update widget
            manager.updateAppWidget(widgetId, views);

            // Return OK
            Intent result = new Intent();
            result.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            setResult(RESULT_OK, result);
            finish();
        }
    });
}
}

主要课程

package com.example.widget;


import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;

public class Main extends AppWidgetProvider {

// Text to be displayed on the widget
static String WIDGET_TEXT;
int widgetId;
RemoteViews views;

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

    for (int i = 0; i < appWidgetIds.length; i++) {
        widgetId = appWidgetIds[i];

        // Get text from preferences
        SharedPreferences quotePreferences = PreferenceManager.getDefaultSharedPreferences(context);
        WIDGET_TEXT = appPreferences.getString("WT"+widgetId, "No text yet.");

        // Update view
        views = new RemoteViews(context.getPackageName(), R.layout.main);
        views.setTextViewText(R.id.widgetText, WIDGET_TEXT);
        appWidgetManager.updateAppWidget(widgetId, views);
    }
}


@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);
}
}

目前onReceive无效,但我也尝试过以下无效。

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    ComponentName thisWidget = new ComponentName(context.getPackageName(), Main.class.getName());
    int [] appWidgets = manager.getAppWidgetIds(thisWidget);
    onUpdate(context, manager, appWidgets);
}

清单

    <receiver android:name="com.example.widget.Main">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/widget"/>
    </receiver>

    <activity android:name="com.example.widgetConfig" android:theme="@android:style/Theme.Dialog">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        </intent-filter>
    </activity>

再次感谢

0 个答案:

没有答案