在ConfigurationActivity之前调用Android小部件的onUpdate()方法

时间:2016-03-18 11:20:34

标签: android configuration widget

Android小部件在ConfigurationActivity之前调用onUpdate()方法,因此我收到黑屏而不是配置屏幕。

这是我的代码。

public class WidgetConfig extends Activity {

    int widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
    Intent resultValue;

    final String LOG_TAG = "FlashLightLogs";

    public final static String WIDGET_TEXT_ENABLED = "widget_text_enabled_";
    public final static String WIDGET_HAS_FLASH = "widget_has_flash_";
    public final static String WIDGET_PREF = "widget_pref";

    private boolean hasFlash;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);


        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            widgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }


        if (widgetID == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }


        resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);

        setResult(RESULT_CANCELED, resultValue);

        setContentView(R.layout.widget_config);
    }

    public void onClick(View v) {

        CheckBox showLabelCheckbox = (CheckBox) findViewById(R.id.widget_show_text_checkbox);
        hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

        SharedPreferences sp = getSharedPreferences(WIDGET_PREF, MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(WIDGET_TEXT_ENABLED + widgetID, showLabelCheckbox.isChecked());
        editor.putBoolean(WIDGET_HAS_FLASH + widgetID, hasFlash);

        editor.commit();


        setResult(RESULT_OK, resultValue);

        Log.d(LOG_TAG, "finish config " + widgetID);
        finish();
    }
}

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pack"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-feature android:name="android.hardware.camera" />
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">

        <activity
            android:name=".WidgetConfig"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:exported="true"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>


        <receiver android:name="com.example.pack.MyProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.intent.action.SCREEN_OFF" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/app_widget" />
        </receiver>
        <receiver android:name="com.example.pack.MyReceiver">
            <intent-filter>
                <action android:name="COM_FLASHLIGHT"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

在我的app_wifget.xml中,我写了android:configure

android:configure="com.example.pack.WidgetConfig"

当我尝试在主屏幕上添加小部件时 - 我得到了黑屏并且在调试时我查看该程序调用小部件的onUpdate。

public class MyProvider extends AppWidgetProvider {
    RemoteViews switchButton;

    private static boolean hasFlash;

    final static String LOG_TAG = "FlashLightLogs";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        /*
         * First check if device is supporting flashlight or not
         */

        SharedPreferences sp = context.getSharedPreferences(
                WidgetConfig.WIDGET_PREF, Context.MODE_PRIVATE);

        if(sp != null) {
            for (int id : appWidgetIds) {
                updateWidget(context, appWidgetManager, sp, id);
            }
        }

        super.onUpdate(context, appWidgetManager, appWidgetIds);


    }

      static void updateWidget(Context context, AppWidgetManager appWidgetManager,
                             SharedPreferences sp, int widgetID) {
        Log.d(LOG_TAG, "updateWidget " + widgetID);

        hasFlash = sp.getBoolean(WidgetConfig.WIDGET_HAS_FLASH + widgetID, false);
        if (!hasFlash) {
            return;
        }
        boolean showLabel = sp.getBoolean(WidgetConfig.WIDGET_TEXT_ENABLED + widgetID, false);

        // Widgets do not support ButtonInput directly so, we need to use RemoteViews.
        RemoteViews switchButton = new RemoteViews(MainActivity.PACKAGE_NAME, R.layout.widget);
        RemoteViews widgetLabel = new RemoteViews(MainActivity.PACKAGE_NAME, R.id.editText);

        if(!showLabel) {
            widgetLabel.setTextViewText(R.id.editText, "");
        }
        // Setting FlashlightAppWidgetReceiver as receiver.
        Intent intent = new Intent(context, FlashlightAppWidgetReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        //Add listener to button.
        switchButton.setOnClickPendingIntent(R.id.widget_button, pendingIntent);

        appWidgetManager.updateAppWidget(widgetID, switchButton);

    }
}

0 个答案:

没有答案