我正在尝试构建一个应用程序,其中相机闪光将在通话和短信传入活动中发生。但是在通话期间相机闪光灯没有打开......我写了简单的程序。
public class MainActivity extends Activity {
.....
....
public static class AlertHandler extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("===inside oneceive");
boolean incomingCall = false;
Parameters alertParams;
Camera alertCamera;
String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (null != callState && callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
incomingCall = true;
}
System.out.println("===incomingCall =="+incomingCall);
if(incomingCall){
System.out.println("OPENING CAMERA...");
alertCamera = Camera.open();
alertParams = alertCamera.getParameters();
alertParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
alertCamera.setParameters(alertParams);
alertCamera.startPreview();
}
}
}
}
我正在使用sysout OPENING CAMERA,但现在闪存了。 请帮忙解决一些代码问题。 logcat没有错误。
在游戏商店这个应用程序的很多存在,它们是如何工作的?
答案 0 :(得分:1)
使用它的工作
public class MainActivity extends ActionBarActivity {
private static Camera camera;
// flag to detect flash is on or off
private static boolean isLighOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = Camera.open();
}
public static void toggle() {
if (isLighOn) {
Log.i("info", "torch is turn off!");
final Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
} else {
Log.i("info", "torch is turn on!");
final Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
}
}
public class AlertHandler extends BroadcastReceiver {
boolean incomingCall = false;
@Override
public void onReceive(Context context, Intent intent) {
String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (null != callState
&& callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
incomingCall = true;
}
if (incomingCall) {
MainActivity.toggle();
} else {
MainActivity.toggle();
}
}
}