Я не могу устранить это исключение безопасности, помогите пожалуйста:
java.lang.RuntimeException:
at android.app.ActivityThread.performDestroyActivity (ActivityThread.java:5567)
at android.app.ActivityThread.handleDestroyActivity (ActivityThread.java:5597)
at android.app.servertransaction.DestroyActivityItem.execute (DestroyActivityItem.java:44)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState (TransactionExecutor.java:190)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:105)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2386)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:213)
at android.app.ActivityThread.main (ActivityThread.java:8178)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1101)
Caused by: java.lang.SecurityException:
at android.os.Parcel.createException (Parcel.java:2071)
at android.os.Parcel.readException (Parcel.java:2039)
at android.os.Parcel.readException (Parcel.java:1987)
at android.media.IAudioService$Stub$Proxy.setStreamVolume (IAudioService.java:2180)
at android.media.AudioManager.setStreamVolume (AudioManager.java:1193)
**at sk.alfa.alias.VoiceControl.muteAudio (VoiceControl.java:253)**
at sk.alfa.alias.MainActivity.onDestroy (MainActivity.java:289)
at android.app.Activity.performDestroy (Activity.java:8350)
at android.app.Instrumentation.callActivityOnDestroy (Instrumentation.java:1348)
at android.app.ActivityThread.performDestroyActivity (ActivityThread.java:5552)
Caused by: android.os.RemoteException:
at com.android.server.audio.AudioService.setStreamVolume (AudioService.java:2530)
at com.android.server.audio.AudioService.setStreamVolume (AudioService.java:2359)
at android.media.IAudioService$Stub.onTransact (IAudioService.java:947)
at com.android.server.audio.HwAudioService.onTransact (HwAudioService.java:1057)
at android.os.Binder.execTransactInternal (Binder.java:1028)
Проблема в строке VoiceControl. java: 253 - audio.setStreamVolume (AudioManager.STREAM_NOTIFICATION, actVolume, 0); :
public class MainActivity extends AppCompatActivity{
static int REQ_CODE_NO_BEEPS = 1;
requestDoNotDisturbPermission();
private void requestDoNotDisturbPermission() {
if(audio.getVolumeFixedPolicy()) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
if (notificationManager.isNotificationPolicyAccessGranted()) {
StaticUtils.save(context,"volume_policy",true);
} else {
StaticUtils.save(context,"volume_policy",false);
}
}else{
StaticUtils.save(context,"volume_policy",true);
}
}
if(!StaticUtils.getBool(context,"volume_policy")){
volumePolicyText.setVisibility(View.VISIBLE);
volumePolicyText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Ask the user to grant access
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult(intent, MainActivity.REQ_CODE_NO_BEEPS);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MainActivity.REQ_CODE_NO_BEEPS) {
if(audio.getVolumeFixedPolicy()){
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
if (notificationManager.isNotificationPolicyAccessGranted()) {
StaticUtils.save(context,"volume_policy",true);
}
}
volumePolicyText.setVisibility(View.GONE);
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
}
}
public VoiceControl(Context context) {
this.context = context;
audio = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
assert audio != null;
actVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
}
public boolean getVolumeFixedPolicy(){
volumeFixedPolicy = audio.isVolumeFixed();
return volumeFixedPolicy;
}
public void muteAudio(Boolean muted){
boolean notificationValid = true;
if(getVolumeFixedPolicy()){
if(!StaticUtils.getBool(context,"volume_policy")){
notificationValid = false;
}
}
if(notificationValid){
if(muted){
audio.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE, AudioManager.FLAG_VIBRATE);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, AudioManager.FLAG_VIBRATE);
}else{
audio.setStreamVolume(AudioManager.STREAM_NOTIFICATION, actVolume, 0);
audio.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_UNMUTE, 0);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, actVolume, 0);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_UNMUTE,0);
}
}
}
Здесь в функции muteAudio я также использую решение для политики фиксированного объема, где я добавил в манифест <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
. Когда необходимо программно включить звук, мне нужно установить конкретную громкость c для УВЕДОМЛЕНИЯ. Некоторые устройства выдают эти RuntimeExceptions и SecurityException, что не так?