请求marshmallow创建手电筒应用程序的权限

时间:2016-08-31 20:44:56

标签: android android-6.0-marshmallow android-permissions flashlight

我创建了一个Flashlight应用。它适用于除棉花糖外的所有设备。即使在我使用了许可后。我觉得在要求我的代码许可时出现了问题。你看到有什么问题吗?

    package com.funny.flashlight;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
    private static final int REQUEST_CAMERA = 0;
    ImageButton imageButton;
    Camera camera;
    Camera.Parameters parameters;
    boolean isflash=false;
    boolean isOn=false;

    public void showCamera(View view)
    {
              if(checkSelfPermission(Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED)
        {
            showCameraPreview();
        }
        else
        {
            if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA))
            {
                Toast.makeText(this, "Camera Permission is needed to show Camera Preview",Toast.LENGTH_SHORT).show();
            }
            requestPermissions(new String[]{Manifest.permission.CAMERA},REQUEST_CAMERA);
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode==REQUEST_CAMERA)
        {
            if (grantResults[0]==PackageManager.PERMISSION_GRANTED)
            {
                showCameraPreview();
            }
            else {
                Toast.makeText(this, "Permission was not Granted", Toast.LENGTH_SHORT).show();
            }
        }
        else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageButton = (ImageButton) findViewById(R.id.imageButton);
        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)
                {
                    if (isOn)
                    {
                        imageButton.setImageResource(R.drawable.off);
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                        camera.setParameters(parameters);
                        camera.stopPreview();
                        isOn = false;
                    }
                    else
                    {

                        imageButton.setImageResource(R.drawable.on);
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        camera.setParameters(parameters);
                        camera.startPreview();
                        isOn = true;
                    }

                }
                else
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Error");
                    builder.setMessage("FlashLight is not Available 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;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

根据您要执行的操作,我建议您按照此格式检查/请求权限:

$ clang test.c
$ ./a.out
Rows: 24, Cols: 80
$ lldb ./a.out
(lldb) target create "./a.out"
Current executable set to './a.out' (x86_64).
(lldb) r
Process 32763 launched: './a.out' (x86_64)
Rows: 0, Cols: 0
Process 32763 exited with status = 0 (0x00000000)

然后检索回复:

    private int CAMERA_PERM = 0;


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERM);
    }