Suphachok Phetthong Suphachok Phetthong - 12 days ago 5
Android Question

How to make an Android flashlight blink when device show result on screen?

If I want to make the device flashlight blink when device show result on screen.

I try this code for blinking the flash, but it is not blinking.

some parts of MainActivity.java

public void onResult(String result) {
String tres = "\n";
String myString = "0101010101";
long blinkDelay = 50;
try {
JSONObject j = new JSONObject(result);
JSONObject j1 = j.getJSONObject("status");
int j2 = j1.getInt("code");
if(j2 == 0){
JSONObject metadata = j.getJSONObject("metadata");
if (metadata.has("custom_files")) {
JSONArray musics = metadata.getJSONArray("custom_files");
for(int i=0; i<musics.length(); i++) {
JSONObject tt = (JSONObject) musics.get(i);
String title = tt.getString("title");
tres = tres + (i+1) + ". Title: " + title + "\n";
}
//Flash Light blink
for (int i = 0; i < myString.length(); i++) {
if (myString.charAt(i) == '0') {
params.setFlashMode(Parameters.FLASH_MODE_ON);
} else {
params.setFlashMode(Parameters.FLASH_MODE_OFF);
}
try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}else{
tres = result;
}
} catch (JSONException e) {
tres = result;
e.printStackTrace();
}

mResult.setText(tres);
}


permission of Manifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />


How can I do this?

Thanks!

Answer

There is issue with Flashlight permissions in manifest.

I am suggesting you to check suggested link for flashlight on/off: Flashlight On/Off