gpt4 book ai didi

android - 在主要 Activity 中请求权限

转载 作者:行者123 更新时间:2023-11-29 15:39:39 24 4
gpt4 key购买 nike

我有一个应用程序可以在设备上录制和存储音频。我的主要 Activity 包含一堆其他 Activity 的按钮。这些其他 Activity 是进行实际录音的 Activity 。

我的主要 Activity 设置如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button mSmallVowel;
private Button mSayCat;
private Button mCough;
private Button mSaySentence;

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

String sep = File.separator;
String newFolder = "AudioRecordingTest";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + sep + newFolder);
myNewFolder.mkdir();

mSmallVowel = (Button) findViewById(R.id.btnSmallVowel);
mSmallVowel.setOnClickListener(this);
mSayCat = (Button) findViewById(R.id.btnSayCat);
mSayCat.setOnClickListener(this);
mCough = (Button) findViewById(R.id.btnCough);
mCough.setOnClickListener(this);
mSaySentence = (Button) findViewById(R.id.btnSaySentence);
mSaySentence.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnSmallVowel:
Intent small_vowel_intent = new Intent(this, SmallVowel.class);
this.startActivity(small_vowel_intent);
break;
case R.id.btnSayCat:
Intent say_cat_intent = new Intent(this, SayCat.class);
this.startActivity(say_cat_intent);
break;
case R.id.btnCough:
Intent cough_intent = new Intent(this, Cough.class);
this.startActivity(cough_intent);
break;
case R.id.btnSaySentence:
Intent say_sentence_intent = new Intent(this, SaySentence.class);
this.startActivity(say_sentence_intent);
break;
}
}
}

所有其他 Activity 如下所示:

public class SaySentence extends AppCompatActivity {

private MediaRecorder recorder;
private String OUTPUT_FILE;
private TextView mStatus;
private TextView mRecorded;

//folder initialization
private String sep = File.separator;
private String mChildFolder = "SaySentence";
private String mExtStorageDirectory = Environment.getExternalStorageDirectory().toString();
private File mChildFile = new File(mExtStorageDirectory + sep + "AudioRecordingTest" + sep + mChildFolder);

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

mChildFile.mkdir();

mStatus = (TextView) findViewById(R.id.txtStatus);
mRecorded = (TextView) findViewById(R.id.txtRecorded);

}

public void startSpeaking(View view) {
//clear text
mRecorded.setText("");

try {
beginRecording();
} catch (Exception e) {
e.printStackTrace();
}
}

public void stopSpeaking(View view) {
try {
stopRecording();
} catch (Exception e) {
e.printStackTrace();
}
}

private void beginRecording() {
ditchMediaRecorder();

//get number of files already in sub-folder and increment the audio filename accordingly
File[] filesInFolder = mChildFile.listFiles();
if (filesInFolder != null) {
int count = filesInFolder.length;
OUTPUT_FILE = mExtStorageDirectory + sep + "AudioRecordingTest" + sep + mChildFolder + sep + "saysentence" + (count+1) + ".3gpp";
}
else {
OUTPUT_FILE = mExtStorageDirectory + sep + "AudioRecordingTest" + sep + mChildFolder + sep + "saysentence0" + ".3gpp";
}

File outFile = new File(OUTPUT_FILE);

mStatus.setText("Recording");

if(outFile.exists()){
outFile.delete();
}

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);

try {
recorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}

recorder.start();

}

private void stopRecording() {
mStatus.setText("Recording Stopped");
mRecorded.setText("Audio file saved to: " + OUTPUT_FILE);

if(recorder != null){
recorder.stop();
}

}

private void ditchMediaRecorder() {
if(recorder != null) {
recorder.release();
}
}
}

由于在 Android 6.0 中必须在运行时询问权限,所以我想这样做以便主要 Activity 请求我在 list 中需要的所有权限:

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

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher_medical"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

我看过官方文档 Requesting Permissions at Run Time ,然而,官方教程让我似乎需要在实际录制和保存音频的 Activity 中请求权限。

我想让 MainActivity 请求所有必需的权限,一旦用户启用它们,它就允许他们毫无问题地使用应用程序的其余部分。

我怎样才能做到这一点?

最佳答案

添加这些行以检查代码中的多个权限:

public static final int MULTIPLE_PERMISSIONS = 100;

// function to check permissions
private void checkPermission() {
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.RECORD_AUDIO) + ContextCompat
.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE))
!= PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale
(getActivity(), Manifest.permission.RECORD_AUDIO) ||
ActivityCompat.shouldShowRequestPermissionRationale
(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(
new String[]{Manifest.permission
.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE},
MULTIPLE_PERMISSIONS);
}

} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(
new String[]{Manifest.permission
.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE},
MULTIPLE_PERMISSIONS);
}
}
} else {
// put your function here

}
}


// Function to initiate after permissions are given by user
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {

switch (requestCode) {
case MULTIPLE_PERMISSIONS:
if (grantResults.length > 0) {
boolean recordPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean writeExternalFile = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if(recordPermission && writeExternalFile)
{
// put your function here
}
}
else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(
new String[]{Manifest.permission
.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},
MULTIPLE_PERMISSIONS);
}
}

}
}

关于android - 在主要 Activity 中请求权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com