2

ホーム画面でウィジェットをクリックすると、フラッシュ ライトが点灯し、「ウィジェットの読み込みに問題があります」というメッセージが表示されます。誰かがこの問題について何か知っていますか?

ウィジェットをクリックする前/後:

ここに画像の説明を入力

ここに画像の説明を入力

res/xml/flashlight_appwidget_info.xml

    <?xml version="1.0" encoding="utf-8" ?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="50dp"
    android:initialLayout="@layout/flash_widget"
    android:updatePeriodMillis="1000"
    android:minHeight="50dp"
/>

res/layout/flash_widget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button"
        android:layout_height="50dp"
        android:layout_width="50dp"
        android:background="@drawable/ic_launcher" />

</LinearLayout>

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver android:name=".FlashlightWidgetProvider" android:icon="@drawable/ic_launcher" android:label="@string/app_name">
         <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
         </intent-filter>

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

    <receiver android:name="FlashlightWidgetReceiver">
            <intent-filter>
                   <action android:name="COM_FLASHLIGHT"></action>
            </intent-filter>
    </receiver>

FlashlightWidgetProvider.java

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

        Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
        receiver.setAction("COM_FLASHLIGHT");
        receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(),
                        R.layout.flash_widget);
        views.setOnClickPendingIntent(R.id.button, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, views);

}

FlashlightWidgetReceiver

   public class FlashlightWidgetReceiver extends BroadcastReceiver {
    private static boolean isLightOn = false;
    private static Camera camera;

    @Override
    public void onReceive(Context context, Intent intent) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.flash_widget);

            if(isLightOn) {
                    views.setImageViewResource(R.id.button, R.drawable.ic_launcher);
            } else {
                    views.setImageViewResource(R.id.button, R.drawable.ic_launcher);
            }

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            appWidgetManager.updateAppWidget(new ComponentName(context,     FlashlightWidgetProvider.class),
                                                                             views);

            if (isLightOn) {
                    if (camera != null) {
                            camera.stopPreview();
                            camera.release();
                            camera = null;
                            isLightOn = false;
                    }

            } else {
                    // Open the default i.e. the first rear facing camera.
                    camera = Camera.open();

                    if(camera == null) {
                            Toast.makeText(context, "Keine Kamera", Toast.LENGTH_SHORT).show();
                    } else {
                            // Set the torch flash mode
                            Parameters param = camera.getParameters();
                            param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                            try {
                                    camera.setParameters(param);
                                    camera.startPreview();
                                    isLightOn = true;
                            } catch (Exception e) {
                                    Toast.makeText(context, "Keine Kamera", Toast.LENGTH_SHORT).show();
                            }
                    }
            }
    }
}
4

0 に答える 0