我在AUDIO_RECORD权限上遇到了问题
简而言之:-AUDIO_RECORD权限始终返回false。
当用户启动应用程序(第一时间)时,它会显示许可请求 消息对话框 如果用户接受,则否认一切正常。 下次用户打开应用程序时,我检查是否授予了权限,它返回了拒绝的权限,然后我再次请求权限。 并在每次启动麦克风时进行录音。它返回被拒绝的权限,我必须再次请求权限。因此,每次我启动麦克风时,它都会返回拒绝权限。 我的问题是该应用程序应在后台运行并从一个小部件开始记录,例如,但是启动权限请求后,该活动将放在最前面。
int permissionResult = PermissionChecker.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
// Start listening to voices
if (permissionResult == PermissionChecker.PERMISSION_DENIED || permissionResult == PermissionChecker.PERMISSION_DENIED_APP_OP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions( new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_RECORD_AUDIO_PERMISSION);
}
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECORD_AUDIO)) {
showPermissionMessageDialog();
} else if (permissionResult == PermissionChecker.PERMISSION_GRANTED) {
//granted.
}
private void showPermissionMessageDialog() {
MessageDialogFragment
.newInstance(getString(R.string.permission_message))
.show(getSupportFragmentManager(), FRAGMENT_MESSAGE_DIALOG);
}
MessageDialogFragment
/**
* A simple dialog with a message.
*
* <p>The calling {@link android.app.Activity} needs to implement {@link
* MessageDialogFragment.Listener}.</p>
*/
public class MessageDialogFragment extends AppCompatDialogFragment {
public interface Listener {
/**
* Called when the dialog is dismissed.
*/
void onMessageDialogDismissed();
}
private static final String ARG_MESSAGE = "message";
/**
* Creates a new instance of {@link MessageDialogFragment}.
*
* @param message The message to be shown on the dialog.
* @return A newly created dialog fragment.
*/
public static MessageDialogFragment newInstance(String message) {
final MessageDialogFragment fragment = new MessageDialogFragment();
final Bundle args = new Bundle();
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getContext())
.setMessage(getArguments().getString(ARG_MESSAGE))
.setPositiveButton(android.R.string.ok, (dialog, which) -> ((Listener) getActivity()).onMessageDialogDismissed())
.setOnDismissListener(dialogInterface -> ((Listener) getActivity()).onMessageDialogDismissed())
.create();
}
}
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="....">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />