1

My flashlight app works with the S2 S3 and several other android phones no where close to as advanced as the S1 but for some reason can't turn on the S1s flash also this problem has been reported on the droid X and several other droids anyone familiar with making these compatible. Code is below

  public class FlashLightActivity extends Activity {

    private Camera mCamera;
    private Boolean mFlashlight = false;
    private Boolean sound = true;

    private ImageButton button;

    private static MediaPlayer mediaPlayer;

    boolean flashInstalled = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flashlight);

        AdView ad = (AdView) findViewById(R.id.adView);
        ad.loadAd(new AdRequest());

        button = (ImageButton) findViewById(R.id.onoff);

        mediaPlayer = new MediaPlayer();

        Context context = this;
        PackageManager pm = context.getPackageManager();

        // if device support camera?
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            Toast.makeText(this, "Your device does not have FlashLight",
                    Toast.LENGTH_LONG).show();
            return;
        }

        try {
            // Checking if flashplayer is available
            ApplicationInfo ai = pm.getApplicationInfo("com.adobe.flashplayer",
                    0);
            if (ai != null)
                flashInstalled = true;
        } catch (NameNotFoundException e) {
            flashInstalled = false;
        }

        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                if (mCamera != null) {
                    Parameters params = mCamera.getParameters();
                    if (mFlashlight) {
                        params.setFlashMode(Parameters.FLASH_MODE_OFF);
                        button.setImageResource(R.drawable.off_icon);
                        mFlashlight = false;
                    } else {
                        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        button.setImageResource(R.drawable.on_icon);
                        mediaPlayer = MediaPlayer.create(
                                FlashLightActivity.this, R.raw.boom);
                        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                            mediaPlayer.stop();
                        }

                        if (sound) {

                            mediaPlayer.stop();
                            mediaPlayer = null;
                        } else {
                            mediaPlayer.start();

                        }
                        v.setKeepScreenOn(true);

                        mFlashlight = true;
                    }
                    mCamera.setParameters(params);
                }
            }

        });

    }

    @Override
    public void onResume() {
        super.onResume();

        SharedPreferences preferences = getSharedPreferences("pref",
                MODE_PRIVATE);
        sound = preferences.getBoolean("sound", false);

        if (mFlashlight) {
            button.setImageResource(R.drawable.on_icon);
        } else {
            button.setImageResource(R.drawable.off_icon);
        }

        try {
            this.mCamera = Camera.open();
        } catch (Exception e) {
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        if (this.mCamera != null) {
            this.mCamera.release();
            this.mCamera = null;
        }
        this.mFlashlight = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tabs, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_settings:
            Intent menuIntent = new Intent(this, MenuActivity.class);
            startActivity(menuIntent);

            return true;

        default:
            return super.onOptionsItemSelected(item);

        }

    }
}
Nikhil
  • 16,194
  • 20
  • 64
  • 81
  • A couple of things: 1. This does not work on my Samsung Galaxy SII (and God knows why, as I am trying to get it to work myself. My stuff works on a Sony XPeria S but not on the SGS II). 2. You're making the assumption, that all phones support FLASH_MODE_TORCH - but there are some that don't. Did you try FLASH_MODE_ON ? Did you try someone elses flashlight app? If other apps work on your SG SI, then chances are, you're just missing something :) – AgentKnopf Dec 19 '12 at 18:24

1 Answers1

0

The only thing I found that works on the Droid X is the code presented by Siddhpura Amit part way down the page in this answer here: Use camera flashlight in Android. He checks the manufacturer and checks to see if it contains the string "motorola." If so, he has special code that can switch the camera Flash LED on or off. I can verify that it does work as I have a Motorola Droid X and this is code I use in my applications.

Community
  • 1
  • 1