2

我的课程扩展了广播接收器。我想在 ICS 中打开手电筒,只有当你有一个表面视图时才可能。我怎样才能制作一个可以打开手电筒的小部件!谢谢!这个代码是不正确的!请帮助我是初学者安卓!

公共无效onReceive(上下文上下文,意图){远程视图视图=新远程视图(上下文.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();
                  }
4

0 回答 0