如何在扩展BroadcastReceiver的类中使用表面支架打开相机
我的课程扩展了BroadcastReceiver。我想在ICS中打开火炬,只有当你有一个surfaceview时才有可能。如何制作一个可以打开火炬的小部件!谢谢!这段代码是inccorect!请帮助我一个初学者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/50645.html
上一篇: How to Open Camera with surface holder in class which extends BroadcastReceiver
下一篇: How to change color of Android ListView separator line?