问题
public class FlashLightActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Context context = this;
PackageManager packageManager = context.getPackageManager();
// if device support camera?
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
//yes
Log.i("camera", "This device has camera!");
}else{
//no
Log.i("camera", "This device has no camera!");
}
}
}
This is a working code for Checking weather the application has FlashLight
or not , but how can I use this code in an appwidgetprovider
?
回答1:
If you are planing to use it inside any function like onUpdate
or onEnabled
etc of Appwidgetprovider
, then all those functions have context
as a input parameter. You can use that context for using PackageManager
as you are doing here.
Also in your question you mention flashlight
. So just check if you need FEATURE_CAMERA_FLASH
or FEATURE_CAMERA
.
Context context = this;
PackageManager packageManager = context.getPackageManager();
// if device support flash?
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
//yes
Log.i("camera", "This device has flash supported!");
}else{
//no
Log.i("camera", "This device has no flash support!");
}
Hope it helps.
来源:https://stackoverflow.com/questions/18242315/check-if-a-device-has-flashlight