Tob*_*oby 2 permissions android 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);
}
}
Run Code Online (Sandbox Code Playgroud)
正如评论中所指出的,出于明显的安全原因,普通的 Android 应用程序无法做到这一点。
通常,对于“测试各种手机功能”,我们不是编写应用程序,而是使用仪器测试的测试套件。在那里,您可以使用 UiAutomator 自动点击权限对话框。但是,这只会作为测试套件的一部分从 Android SDK 安装中运行。
或者,在您的情况下,将您的设置设置为targetSdkVersion23 以下,您将不需要处理运行时权限。最终,某些事情会迫使您的手牌高于targetSdkVersion此值,但您可能能够“将罐子推倒”并在未来应对该挑战。
| 归档时间: |
|
| 查看次数: |
2548 次 |
| 最近记录: |