Suppose an app starts, and it is unable to detect the device camera.
Then I want to show not find camera!!
dialog
I think to use UnhandledExceptionHandler
in onCreate
Is it correct or something else would be better ?
Suppose an app starts, and it is unable to detect the device camera.
Then I want to show not find camera!!
dialog
I think to use UnhandledExceptionHandler
in onCreate
Is it correct or something else would be better ?
You can wrap Camera.open()
in try … catch. Some other camera APIs, including setParameters(), can throw RuntimeException, too.
It's not a good idea to wrap all of your Activity's onCreate() in one huge try … catch: you want the activity to get correctly created even if the camera fails, at least so that it can host the dialog.
By the way, the good practice is not to call Camera.open()
from onCreate()
, but rather use a background Handler thread.
Alternatively you can also do it like this :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesn't support camera",
Toast.LENGTH_LONG).show();
// will close the app if the device does't have camera
finish();
}
}
private boolean isDeviceSupportCamera() {
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
}