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); } } 
1
  • Is your goal to share code, or to make it as if the other activity (rather than the calling one) did these things? Commented Oct 26, 2014 at 14:41

2 Answers 2

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(); } } 
Sign up to request clarification or add additional context in comments.

16 Comments

Tnx a lot. But the thing is, AudioManagerHolder should be on another .java file. How can I call from there?
I updated the code, you probably need a book to get some introductory concepts down.
at edu.shihank.audiomanager.MainActivity.onCreate(MainActivity.java:22) says your MainActivity line 22 has problem.
@SinOscuras: :) good.. believe it or not, many of us have gone through the same pain.. what's not working now?
so you need context (or activity) to call getSystemService. updated the code.
|
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); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.