Reputation: 151
I made an app for testing various phone functions with no user interaction, but it was not working on some devices, even though it worked fine before. My theory was that it needed runt-time permissions and I was right. When I put the run-time permission in the activity, it works perfectly. The problem is that it requests the permissions from the user and I can't have user interaction. Is there a way to grant permissions to the activity without user interaction?
I will include an activity where I got the recorder activity to work, but is dependent on the user granting permission. Please help!!
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);
}
}
Upvotes: 2
Views: 4571
Reputation: 3622
Yea it is possible but its have a some limitations .
1) You have to use sdk 22 or below , if you use sdk above 22 than it will ask you run time permission.
i tried this method its works !
here sample project link :-https://github.com/kdblue/PermissionGranted
Upvotes: 0
Reputation: 254
This probably isn't what you want, but a user can technically grant permissions without interacting with your app. In Android 6+ users can grant and deny individual permissions through the phone Settings -> Apps -> Permissions. Technically this grants the permission without interacting with the app.
Upvotes: 2
Reputation: 1006674
As is noted in the comments, this is not possible from ordinary Android apps, for blindingly obvious security reasons.
Frequently, for "testing various phone functions", we do not write an app, but a test suite using instrumentation testing. There, you could use UiAutomator to automate clicking on the permission dialog. However, this will only run as part of a test suite, from an Android SDK installation.
Alternatively, in your case, set your targetSdkVersion
below 23, and you will not need to deal with runtime permissions. Eventually, something will force your hand to have a higher targetSdkVersion
than that, but you might be able to "kick the can down the road" and deal with that challenge in the future.
Upvotes: 6
Reputation: 2447
Unfortunately, this can't be done, because that would negate the purpose of permissions.
Perhaps if you had/wrote an app that could physically tap the permission box's allow button, ie. Something that can overlay other apps and interact with what's on the screen. But with something like that, I can't help you.
Upvotes: 0