0

I am making an app that mutes and unmutes a device, but it is crashing when I open it. I have no idea why it is doing this and would really like some help. Here is the code:


package com.earthmonster.testmute4;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.content.Context;

import android.media.AudioManager;

import android.os.Bundle;

 

public class MainActivity extends AppCompatActivity {

 

 

 

   

 

 

    AudioManager audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

 

    @Override

 

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

 

 

        AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);

        int volume_level=am.getStreamVolume(AudioManager.STREAM_MUSIC);

        if (volume_level > 0)

        {

            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);

            audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, 0);

            audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);

            audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0);

            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);

 

        }

        else {

            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);

            audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, 100, 0);

            audioManager.setStreamVolume(AudioManager.STREAM_RING, 100, 0);

            audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 100, 0);

            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);

        }

        finish();

    }

}


Can someone please figure out the problem! I am an absolute beginner on this. No java experience. No android developing experience. And mostly compiled this code from various sources on the internet.

Thanks in advance.

isXander
  • 127
  • 1
  • 7

2 Answers2

1

I used DHAVAL ASODARIYA's answer but had to put a bit more in so I will answer myself.

I replaced below variable definition,

AudioManager audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

with this one,

AudioManager audioManager;

And in onCreate method, replace below line,

AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);

with this one,

audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);

I added

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

to the manifest because it required Do Not Disturb Access and added this

NotificationManager notificationManager = 
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
    && !notificationManager.isNotificationPolicyAccessGranted()) {

    Intent intent = new Intent(
                        android.provider.Settings
                        .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);

    startActivity(intent);
}

To check if it had access and if not, went to the page to do it.

This fixed the security exception that caused the fatal error and now the app works flawlessly.

isXander
  • 127
  • 1
  • 7
0

I have checked your code. There is unnecessary variable defined before creating application. You need to make few changes.

Replace below variable definition,

AudioManager audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

with this one,

AudioManager audioManager;

And in onCreate method, replace below line,

AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);

with this one,

audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27
  • It still crashes. – isXander Jun 03 '19 at 06:30
  • Please post crash log. – DHAVAL A. Jun 03 '19 at 06:55
  • @EarthMonster That is the security exception with code. Please have a look at below post. https://stackoverflow.com/questions/39151453/in-android-7-api-level-24-my-app-is-not-allowed-to-mute-phone-set-ringer-mode – DHAVAL A. Jun 03 '19 at 07:06
  • Do I need the same permission as in the post? – isXander Jun 03 '19 at 07:27
  • Yes, you need same permission as new privacy policy has been changes with new version updates. And you are also trying to change notification, ring and system volume with your app, so. – DHAVAL A. Jun 03 '19 at 09:23
  • THANKS THANKS FINALLY IT WORKED!!! One more thing though. How do I show that it is changing? As in the bar at the side shows it has changed. – isXander Jun 03 '19 at 15:03