Android-如何在没有用户交互的情况下授予运行时权限?

时间:2017-01-12 14:00:19

标签: android permissions android-permissions

我制作了一个应用程序,用于测试各种手机功能而无需用户交互,但它在某些设备上无法正常工作,即使之前工作正常。我的理论是它需要时间权限,我是对的。当我在活动中放置运行时权限时,它完美地运行。问题是它请求用户的权限,我无法进行用户交互。有没有办法在没有用户交互的情况下为活动授予权限?

我将包含一项活动,让我的记录器活动起作用,但取决于用户授予权限。请帮忙!!

    public static final int RECORD_AUDIO_PERMISSION_REQUEST = 3;



    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                /*
         * API's to launch the application when the tablet is locked or
         * display is turned off
         */
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
        setContentView(R.layout.activity_recorder);

        //Check to see if the device has a microphone
        PackageManager pm = getPackageManager();
        boolean micPresent = pm.hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
        if (!micPresent) {
            Log.i(log_tag, "There is no microphone present in this device.");
            exit_function();
        } else {
            createTempFile(status_tag, "INPROGRESS");

            //Create the file to write the recording
            try {
                FileOutputStream fOut = openFileOutput("audio_test.3gp", MODE_WORLD_READABLE);
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(log_tag, "FAILED TO CREATE THE FILE OUTPUT STREAM");
                exit_function();
            }

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

            //start_recording();
        }
    }




    //Start the Recording
    private void start_recording() {
        if (recorder != null) {
            recorder.stop();
            recorder.reset();
            recorder.release();
            recorder = null;
        }
        //Setting for the Recorder
        try {
            Log.i(log_tag, "Setting the recorder");
            //This is the path that the file will be saved
            path = getFilesDir().getAbsolutePath() + "/audio_test.3gp";
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(path);

        } catch (Exception e) {
            Log.e(log_tag, "Recording Settings Failed");
            exit_function();
        }
        //Prepare the Recorder
        try {
            Log.i(log_tag, "Preparing the Recorder");
            recorder.prepare();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(log_tag, "Recording failed");
            exit_function();
        }

        //Start the Recorder
        try {
            Log.i(log_tag, "Starting the recorder");
            title_text = ((TextView) findViewById(R.id.textView));
            title_text.setTextColor(Color.RED);
            title_text.setText("RECORDING");
            recorder.start();

            //The recording lasts as long as he timer and then stops
            mHandler.postDelayed(new Runnable() {
                public void run() {
                    if (recorder != null) {
                        recorder.stop();
                        recorder.reset();
                        recorder.release();
                        recorder = null;
                    }
                    Log.e(log_tag, "First Delay");
                    exit_function();
                }
            }, timer);
            createTempFile(status_tag, "Complete");


        } catch (Exception e) {
            e.printStackTrace();
            Log.e(log_tag, "Recorder start failed");
            exit_function();
        }
    }
        @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case  RECORD_AUDIO_PERMISSION_REQUEST:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    start_recording();
                } else {
                    onDestroy();
                }
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

4 个答案:

答案 0 :(得分:4)

正如评论中所指出的那样,出于明显的安全原因,普通的Android应用程序无法做到这一点。

通常,为了测试各种电话功能",我们不会编写应用程序,而是使用仪器测试编写测试套件。 ,您可以使用UiAutomator自动点击权限对话框。但是,这只会作为测试套件的一部分从Android SDK安装中运行。

或者,在您的情况下,将targetSdkVersion设置为低于23,您将不需要处理运行时权限。最终,某些东西会强迫你的手牌targetSdkVersion高于Public Sub Workbook_AfterSave(ByVal Success As Boolean) ,但你可能会把你的手放在路上#34;并在将来处理这一挑战。

答案 1 :(得分:2)

这可能不是您想要的,但用户可以在技术上授予权限,而无需与您的应用进行交互。在Android 6+中,用户可以通过手机设置 - >授予和拒绝个人权限。应用 - >权限。 从技术上讲这样可以在不与应用程序交互的情况下授予权限。

答案 2 :(得分:0)

不幸的是,这不可能做到,因为这会否定权限的目的。

也许如果您有/写过一个可以物理点击权限框的允许按钮的应用程序,即。可以覆盖其他应用程序并与屏幕上的内容进行交互的内容。但有了这样的话,我无法帮助你。

答案 3 :(得分:0)

是的,它有可能,但它有一些局限性。

1)你必须使用sdk 22或​​更低版本,如果你使用22以上的sdk而不是它会问你      运行时权限。

  • 转到app level gradle并输入targetSdkVersion 22。

我尝试了这种方法的作品!

此处示例项目链接: - https://github.com/kdblue/PermissionGranted

相关问题