0

Good day all, I'm new to android development and I'm following THIS tutorial to make a simple audio manager. But I want some functions to be on another activity class and call those functions according to corresponding button press. The functions are:

public void vibrate(View view){
   myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
public void ring(View view){
  myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
public void silent(View view){
  myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
public void mode(View view){
 int mod = myAudioManager.getRingerMode();
 if(mod == AudioManager.RINGER_MODE_NORMAL){
     Status.setText("Current Status: Ring");
}
else if(mod == AudioManager.RINGER_MODE_SILENT){
   Status.setText("Current Status: Silent");
}
else if(mod == AudioManager.RINGER_MODE_VIBRATE){
  Status.setText("Current Status: Vibrate");
}
else{

 }
}

Is there any way I can do that? And I don't want to use Intent. Your help would be an honor for me. Tnx.

UPDATES AFTER LAUNCH

Updated Logcat logs:

10-28 20:57:51.381: W/dalvikvm(1232): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-28 20:57:51.401: E/AndroidRuntime(1232): FATAL EXCEPTION: main
10-28 20:57:51.401: E/AndroidRuntime(1232): java.lang.NullPointerException
10-28 20:57:51.401: E/AndroidRuntime(1232):     at edu.shihank.audiomanager.Listeners.vibrate(Listeners.java:13)
10-28 20:57:51.401: E/AndroidRuntime(1232):     at edu.shihank.audiomanager.MainActivity$1.onClick(MainActivity.java:39)

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    home = (Button) findViewById(R.id.home);
    pocket = (Button) findViewById(R.id.pocket);
    silent = (Button) findViewById(R.id.silent);
    tv = (TextView) findViewById(R.id.tv);

    myAudManHolder = new Listeners();

    pocket.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            myAudManHolder.vibrate();
        }

    });
}

Listeners.java

public class Listeners {
AudioManager myAudMan;

public void vibrate() {
    myAudMan.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
   }
}
Shihan Khan
  • 2,180
  • 4
  • 34
  • 67

2 Answers2

1

create a class which will hold AudioManager

public class MainActivity extends Activity {
    private AudioManagerHolder mAudioManagerHolder;

    public void vibrate() {
        mAudioManagerHolder.vibrate();
    }

    public void mode() {
        int mod= mAudioManagerHolder.getMode();
        if(mod == AudioManager.RINGER_MODE_NORMAL){
            Status.setText("Current Status: Ring");
        }
    }
}

public class AudioManagerHolder {
    private AudioManager mAudioManager; 
    private Context mContext;

    public AudioManagerHolder(Context context) {
       mContext = context;
       mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    }

    public void vibrate(){
        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
    }

    public int getMode() {
        return mAudioManager.getRingerMode();
    }
}
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Tnx a lot. But the thing is, `AudioManagerHolder` should be on another .java file. How can I call from there? – Shihan Khan Oct 26 '14 at 14:48
  • call what from there? – eugene Oct 26 '14 at 14:49
  • call the class `AudioManagerHolder` and it's methods. – Shihan Khan Oct 26 '14 at 14:53
  • your original class has `mAudioManagerHolder` variable, and you call AudioManagerHolder methods using that variable as I have shown. – eugene Oct 26 '14 at 14:54
  • ok I did exactly what you showed me, no error is showing in my code. However now app gets crash when launching. I provided Logcat logs. Please can you explain what is wrong now? Tnx. – Shihan Khan Oct 26 '14 at 15:13
  • Caused by: java.lang.ClassCastException: android.widget.ImageView .. you are probably casting wrong object.. – eugene Oct 26 '14 at 16:29
  • But I've nothing regarding `android.widget.ImageView` :( – Shihan Khan Oct 26 '14 at 16:31
  • where should I put this line `myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);` which is mentioned in the tutorial? – Shihan Khan Oct 26 '14 at 16:34
  • 1
    I updated the code, you probably need a book to get some introductory concepts down. – eugene Oct 27 '14 at 03:06
  • 1
    at edu.shihank.audiomanager.MainActivity.onCreate(MainActivity.java:22) says your MainActivity line 22 has problem. – eugene Oct 27 '14 at 15:21
  • 1
    @SinOscuras: :) good.. believe it or not, many of us have gone through the same pain.. what's not working now? – eugene Oct 28 '14 at 01:41
  • still cant get a work around how to use `myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);` I followed your way and I get an error for `getSystemService` saying `The method getSystemService(String) is undefined for the type Listeners`. Tried to use Context as object still no luck. Any idea? – Shihan Khan Oct 28 '14 at 04:48
  • 1
    so you need context (or activity) to call getSystemService. updated the code. – eugene Oct 28 '14 at 05:44
  • Ok. I finally figured what was the prob. It seems that `getSystemService` only works with Activity classes. Since I had another non-activity class and I used `getSystemService` there so it wasn't working. Now, it launches but I think there is something wrong with my `vibrate()` method and its onclicklistener method. I've updated my code in my ques. Can you tell me what is wrong now? – Shihan Khan Oct 28 '14 at 15:17
  • Nevermind, I got my solution. Check my ans. And tnQ so much for sticking with me for this long. If you've fb please send me a request here: [My FB Profile](https://www.facebook.com/shihank1) or Skype id: sinoscuras – Shihan Khan Oct 28 '14 at 16:08
  • Congrats! I don't fb much. hope SO has a way to keep in touch. :) – eugene Nov 03 '14 at 12:57
0

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    home = (Button) findViewById(R.id.home);
    pocket = (Button) findViewById(R.id.pocket);
    silent = (Button) findViewById(R.id.silent);
    mode = (Button) findViewById(R.id.mode);
    tv = (TextView) findViewById(R.id.tv);

    myAudManHolder = new Listeners(am);

    pocket.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            myAudManHolder.vibrate();
        }
    });
}

Listeners.java

public class Listeners {
AudioManager myAudMan;

Listeners(AudioManager audiomanager){
    this.myAudMan = audiomanager;
}

public void vibrate() {
    myAudMan.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
   }
}
Shihan Khan
  • 2,180
  • 4
  • 34
  • 67