Reputation: 524
To check whether camera is running or not i am writing these code
ActivityManager actvityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
String packageName = actvityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
if(packageName.equals("android.hardware.camera")||packageName.equals("com.android.camera")){
Camera_status = "STATUS_ON";
System.out.println("===on===");
}else{
Camera_status = "STATUS_OFF";
System.out.println("====off====");
}
By using this, i am able to get correct result in android emulator but while testing In real device i an not able to get the correct result.As per the answer posted here by CommonsWare,i think he is correct.So friends may i know how to get the camera on/off status programmatically.
Upvotes: 1
Views: 1170
Reputation: 64
Try This its working!!!
Camera _camera;
boolean qOpened = false;
try {
_camera=Camera.open();
qOpened=(_camera!=null);
if(qOpened){
Camera_status = "STATUS_OFF";
}else{
System.out.println("==nothing to do====");
}
} catch (Exception e) {
Camera_status = "STATUS_ON";
System.out.println("=====Camera running=====");
}
Upvotes: 2