0

I intend to make a small project in which whenever the user clicks on the ringer icon in the notification drop down menu, a dialog box appears notifying the user that some change in the ringer audio has been made.

I really need help to understand this, so please help me by specifying everything in context to the code you might submit, and not just references to what I could do.

public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;

private LinearLayout mRootLayout;
private Button mBtnRingerVolume;
private TextView mTVStats;

Random mRandom = new Random();

private NotificationManager mNotificationManager;
private AudioManager mAudioManager;

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

    // Get the application context
    mContext = getApplicationContext();
    mActivity = MainActivity.this;

    // Get the widget reference from xml layout
    mRootLayout = findViewById(R.id.root_layout);
    mTVStats = findViewById(R.id.tv_stats);
    mBtnRingerVolume = findViewById(R.id.btn_ringer_volume);

    // Get the notification manager instance
    mNotificationManager = (NotificationManager) 
    getSystemService(NOTIFICATION_SERVICE);

    // Get the audio manager instance
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

    mBtnRingerVolume.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // If do not disturb mode on, then off it first
            turnOffDoNotDisturbMode();

            // Get the ringer current volume level
            int current_volume_level = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);

            // Get the ringer maximum volume
            int max_volume_level = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);

    // Get a random volume level in specified range
    int random_volume = mRandom.nextInt(((max_volume_level-0)+1)+0);

    // Set the ringer volume
    mAudioManager.setStreamVolume(
            AudioManager.STREAM_RING,
            random_volume,
            AudioManager.FLAG_SHOW_UI
    );

    // Display the updated status on text view
    mTVStats.setText("Ringer Current Volume : " + current_volume_level);
    mTVStats.append("\nRinger Max Volume : " + max_volume_level);
    mTVStats.append("\nRinger new volume : " + random_volume);
}
});
}

// Custom method to turn off do not disturb mode programmatically
protected void turnOffDoNotDisturbMode(){
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){ // If api level minimum 23
        // If notification policy access granted for this package
        if(mNotificationManager.isNotificationPolicyAccessGranted()){{
            if(mAudioManager.getStreamVolume(AudioManager.STREAM_RING)== 0){
                // If do not disturb mode on, then off it
                // Set the interruption filter to all, allow all notification
                mNotificationManager.setInterruptionFilter(mNotificationManager.INTERRUPTION_FILTER_NONE);
                // Show a toast
                Toast.makeText(mContext,"Turn OFF Do Not Disturb Mode",Toast.LENGTH_SHORT).show();
            }
        }
        }else {
            // Show a toast
            Toast.makeText(mContext,"Going to get grant access",Toast.LENGTH_SHORT).show();

            // If notification policy access not granted for this package
            Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
            startActivity(intent);
        }
    }
}
}

I have made this, which is just a simple app to change the ringer volume to a random number value every time the user presses the button. What I want is to give a dialog box notifying the user that a change has been observed in ringer settings when the user changes it in the notification drop down menu. I hope this clears out the question.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Aqib Ahmed
  • 48
  • 8
  • 1
    Add what code you have tried or what is the error you face – BHUVANESH MOHANKUMAR Jul 03 '18 at 13:51
  • I haven't made any code for this part, per se. I have designed the layout and the UI for the main app. I have even done some work in changing the ringer audio changes. But this part I don't know where to start. – Aqib Ahmed Jul 03 '18 at 14:03
  • @BHUVANESHMOHANKUMAR I have however made the edits to include code for the part where a simple changer is made for the ringer volume level. – Aqib Ahmed Jul 03 '18 at 14:13

0 Answers0