Check if a device has flashlight [closed]

前提是你 提交于 2019-12-26 14:01:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!