0

If i give permission manually than it works. Rather than that it doesn't work. Can any one help me. What change should i made. I am new to android development. I can't find anything on google.

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.plutianclub.flashlight">

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_flashlight_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity"
        android:screenOrientation="portrait">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Main Activity:

package com.plutianclub.flashlight;

import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

ImageButton imageButton;

Camera camera;
Camera.Parameters parameters;
boolean isFlash = false;
boolean isOn = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageButton = (ImageButton) findViewById(R.id.offButton);

    if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
        camera = Camera.open();
        parameters = camera.getParameters();
        isFlash = true;
    }

    imageButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if (isFlash == true){
                if (isOn == false  ){
                    imageButton.setImageResource(R.drawable.on);
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                    camera.startPreview();
                    isOn = true;

                }else {
                    imageButton.setImageResource(R.drawable.off);
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);
                    camera.stopPreview();
                    isOn = false;
                }

            }else {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Error....");
                builder.setMessage("Flashlight is not avilable on this device.");
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

            }
        }
    });
}

@Override
protected void onStop() {
    super.onStop();
    if (camera != null){
        camera.release();
        camera = null;
    }
}
}
  • https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it Also, [there is no `FLASHLIGHT` permission in Android](https://developer.android.com/reference/android/Manifest.permission.html). – CommonsWare May 13 '17 at 17:40

1 Answers1

0

From Android 6.0 (Marshmallow) Android requires that you ask permission by code at runtime or at the time when the feature are gonna be used.

There are more about here with examples. https://developer.android.com/training/permissions/requesting.html

Anything below Android 6.0 will just require as you already have done it in the manifest