Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to turn the flash on the phone (flashlight)?
    text
    copied!<p>There is a code that is to turn on the flash when you press the button, this is why it is not happening. Help me understand why this is not happening, please. Tested the application on samsung galaxy nexus and lg p970.</p> <p>Activity:</p> <pre><code>package com.example.Flashlight; import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.util.Log; import android.view.View; import android.widget.*; public class MainActivity extends Activity { Camera camera; boolean isFlashOn; boolean hasFlash; Parameters params; Button flashlightButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); checkFlashlight(); getCamera(); initFlashlightButton(); } void checkFlashlight() { hasFlash = getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); if (!hasFlash) { AlertDialog alert = new AlertDialog.Builder( MainActivity.this).create(); alert.setTitle("Error"); alert.setMessage("Sorry, your device doesn't support flash light!"); alert.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); alert.show(); return; } } // initialization button and click listener private void initFlashlightButton() { flashlightButton = (Button) findViewById(R.id.flash_light); flashlightButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (isFlashOn) turnOffFlash(); else turnOnFlash(); } }); } private void getCamera() { if (camera == null) { try { camera = Camera.open(); params = camera.getParameters(); } catch (RuntimeException e) { Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); } } } // here must be of the flash (it is not clear why this is not happening) private void turnOnFlash() { if (!isFlashOn) { if (camera == null || params == null) return; params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); isFlashOn = true; } } // flash OFF private void turnOffFlash() { if (isFlashOn) { if (camera == null || params == null) return; params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(params); camera.stopPreview(); isFlashOn = false; } } </code></pre> <p>Layout:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"&gt; &lt;Button android:id="@+id/flash_light" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Flashlight"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Manifest:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.Flashlight" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-permission android:name="android.permission.CAMERA" /&gt; &lt;uses-feature android:name="android.hardware.camera" /&gt; &lt;uses-sdk android:minSdkVersion="8"/&gt; &lt;application android:label="@string/app_name" android:icon="@drawable/ic_launcher"&gt; &lt;activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Black"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN"/&gt; &lt;category android:name="android.intent.category.LAUNCHER"/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload