3

我是 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>

再次感谢

4

0 回答 0