Working with the Android Framework Classes
This section gets into the good stuff — the nitty-gritty of Android development and its Android framework classes! Yes, activities and views are integral parts of the system, but they’re simply the “plumbing” that’s required in any modern operating system (in one capacity or another). The real fun is just about to start.
The following sections describe how to check the state of the phone ringer to determine whether it’s in normal mode (ringing loud and proud) or silent mode. At this point, you can begin to start toggling the phone’s ringer mode.
Getting good service
To access the Android ringer, you’ll need lots of access to the AudioManager in Android, which is responsible for managing the ringer state, so you should initialize it in onCreate().
You first need to create a private class-level AudioManager variable by the name of mAudioManager. Type this name at the top of your class file, directly after the class declaration line, as shown in Listing 5-3.
Listing 5-3: Adding the Class-Level AudioManager Variable
package com.dummies.android.silentmodetoggle;
import android.app.Activity;
import android.media.AudioManager; →4
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private AudioManager mAudioManager; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
