-1
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 ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SamJ
  • 413
  • 1
  • 4
  • 18

1 Answers1

3

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.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • In the the class (extends AppWidgetProvider).I have a method i want to be able to check if the device has flashlight from that method – SamJ Aug 15 '13 at 09:41
  • You mean the capability of giving out flash light from its camera? I think that is what the code is doing? Isn't it? – Shobhit Puri Aug 15 '13 at 14:40