- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序可以在设备上录制和存储音频。我的主要 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/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
Android 权限(您在 list 中请求并在安装时显示的权限)是否与 root 用户在 root 手机上获得的 linux 权限相同? 更确切地说:如果我的手机上有 root 权限并且我有一个可以
我经常读到 VIEW 的一个目的是安全性:允许一些用户访问基础表,而其他用户只允许访问派生 View 。考虑到这一点,我设计了几个向外部用户提供受限数据集的 View 。 一切都很好,但在实践中这是行
在 Facebook API v2.3 中,“user_posts”听起来像是“user_status”的超集。是这样吗?如果我已经有“user_posts”,为什么还需要“user_status”?
在为 BLUETOOTH_CONNECT 请求运行时权限后,android 12 崩溃了,我在 Samsung Android 12 设备中遇到了这个问题。在其他低于 Android 12 的设备上运
请理解这个问题可能有点头晕,因为这是我第一次提问。另外,请理解语法可能很奇怪,因为我不擅长英语并使用翻译。 我是一个在 Android 工作室中使用 java 制作应用程序的人。 尝试使用蓝牙时出现连
我刚刚将我的 Magento 商店从 cPanel 移动到 DirectAdmin (Centos)。 我的问题现在是权限。以前在 cPanel 上,所有文件夹都设置为 755 和文件 644。这很好
我希望在我的 Django 项目中获得更细粒度的权限,但无法决定使用哪个应用程序。 我所拥有的是这样的: class Item(models.Model): name = models.Cha
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我们已经设置了一个 Github 应用程序,以便它使用 Github API 自动为另一个个人 Github 用户创建一个存储库。现在我们遇到了一个问题,不是每个人都想让我们完全读取他们所有的私有(p
假设我有一个网站想要访问 Facebook 的用户帐户信息。通常,用户会获得网站要求的所有权限,并且可以整体上允许或拒绝这些权限。 是否可以让用户选择(例如,通过授权屏幕上每个权限的复选框)他想授予网
平台下载地址:https://gitee.com/alwaysinsist/edp 权限介绍 权限实际上就是谁有权使用或是访问什么,这里的“谁”可以视作"授权对象",&qu
playstore 给我发这个消息 We've detected that your app contains the requestLegacyExternalStorage flag in the
我可以在没有 sudo 的情况下运行 docker,但有时它会再次请求权限,我无法在 VS 代码中附加容器 Got permission denied while trying to connect
我正在尝试在 Ubuntu 中的可执行文件上运行 gdb。但是,当我尝试在 gdb 中运行 run 时,出现以下错误。 /vagrant/unit_test: cannot execute: Perm
我的应用程序工作了一年,然后对 instagram 的 API 调用停止返回任何数据。 我使用以下 instagram 端点: https://api.instagram.com/v1/media/s
我使用 TFS 2012 并希望为 TFS 用户组设置以下权限。 允许创建新问题项。 拒绝创建新的任务项。 拒绝更改他的任务项,只能更改提醒时间、描述和状态。并且不能更改分配的用户、优先级和迭代。 我
我有一个谷歌计算引擎实例,我使用与我的 glcoud 帐户关联的 SSH key 通过 SFTP 连接到该实例。但是,我无法将任何文件上传到/var/www 目录,尽管我可以读取目录列表。/var/w
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我不确定如何正确处理以下情况: 我的程序通过安装程序安装 我在应用程序文件夹中创建SQLite数据库(程序启动时) 在某些配置中,我收到“ Attemt写入只读数据库”错误。这是权限问题,现在我通过将
我是一名优秀的程序员,十分优秀!