手电筒小部件无法关闭

时间:2014-02-05 14:21:45

标签: android camera android-widget widget android-appwidget

我创建了我的小部件存在和运行所需的一切。即便如此,在第一次点击时,t会做它应该做的事情但是然后图像会改变并说出问题,并且不起作用。我希望它打开闪存然后关闭它。 将非常感谢帮助。

FlashlightWidgetProvider

public class FlashlightWidgetProvider extends AppWidgetProvider {

       @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.off);
                } else {
                        views.setImageViewResource(R.id.button, R.drawable.on);
                }

                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, R.string.no_camera, 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, R.string.no_flash, Toast.LENGTH_SHORT).show();
                                }
                        }
                }
        }
}

1 个答案:

答案 0 :(得分:2)

确保按钮资源引用ImageView而不是常规Button。我刚开始尝试使用我的布局文件中的一个按钮,我遇到了同样的问题,即小部件基本上会崩溃并从主屏幕中移除。当我将按钮更改为布局文件中的ImageView时,代码现在可以正常工作。

我确实修改了你的代码,所以如果它本身不起作用,这里是更新的FlashlightWidgetProvider:

public class FlashlightWidgetProvider extends AppWidgetProvider {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction())) {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));

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

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

            appWidgetManager.updateAppWidget(appWidgetIds, views);
        }

        super.onReceive(context, intent);
    }
}

此外,请确保在清单中正确注册窗口小部件提供程序和接收器(当然,用您自己的部分替换相关部分):

    <receiver
        android:name="com.example.stackoverflowtester.widget.FlashlightWidgetProvider"
        android:label="Flashlight" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

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