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"
/>

资源/布局/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);

}

手电筒小部件接收器

   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