-2
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!");
        }


    }
}

这是用于检查应用程序是否有天气的工作代码FlashLight,但我如何在其中使用此代码appwidgetprovider

4

1 回答 1

2

如果您打算在诸如onUpdateor之类onEnabled的任何函数中使用它Appwidgetprovider,那么所有这些函数都将context作为输入参数。PackageManager您可以像在此处一样使用该上下文。

同样在您提到的问题中flashlight。因此,只需检查您是否需要FEATURE_CAMERA_FLASHFEATURE_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!");
}

希望能帮助到你。

于 2013-08-14T21:14:30.167 回答