How to Open Camera with surface holder in class which extends BroadcastReceiver

My Class extends broadcastReceiver .I want to turn on torch in ICS ,and it is posible only if you have a surfaceview.How can I make a widget which can turn on torch !Thanks!This code is inccorect!Please help Im a beginner in Android!

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

        if(isLightOn) {

            Log.d(tag, "IS LIGHT ON");
        } else {

            Log.d(tag, "IS LIGHT OFF");
        }

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

        if (isLightOn) {
                if (camera != null) {
                        camera.setPreviewCallback(null);
                        camera.stopPreview();
                        camera.release();
                        camera = null;
                }
                isLightOn = false;
        } else {
                //CAMERa OPEN 


            numberOfCameras = Camera.getNumberOfCameras();

            // Find the ID of the default camera
            CameraInfo cameraInfo = new CameraInfo();
            for (int i = 0; i < numberOfCameras; i++)
            {
                Camera.getCameraInfo(i, cameraInfo);
                if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK)
                {   

                    defaultCameraId = i;

                }
            }

            try{
                 // Open the default i.e. the first rear facing camera.

                camera = Camera.open(defaultCameraId);
                camera.setDisplayOrientation(90);
                mPreview.setCamera(camera);


            } catch( Exception e ){
                Log.e("RECEIVER", "No camera");


                mPreview = new CameraPreview(context, camera);
                LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);


                LinearLayout layout_main = new LinearLayout(context);
                layout_main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

                layout_main = (LinearLayout) inflater.inflate(R.layout.camera_widget, layout_main);


                FrameLayout frameLayout = (FrameLayout) layout_main.findViewById(R.id.camera_widget_frame);
                CameraPreview mPreview = new CameraPreview(context, camera);
                mPreview.setCamera(camera);
                frameLayout.addView(mPreview);

        }

                        Parameters param = camera.getParameters();
                        param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        camera.setParameters(param);
                        camera.startPreview();
                  }
链接地址: http://www.djcxy.com/p/50646.html

上一篇: Android小部件onReceive崩溃

下一篇: 如何在扩展BroadcastReceiver的类中使用表面支架打开相机