从android中的另一个活动调用一个函数

时间:2014-10-26 14:16:25

标签: java android

美好的一天,我是Android开发的新手,我跟随THIS tutorial创建了一个简单的音频管理器。但是我想要一些函数在另一个活动类上,并根据相应的按钮按下来调用这些函数。功能是:

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{

 }
}

我有什么方法可以做到吗?而且我不想使用Intent。你的帮助对我来说是一种荣幸。 TNX。

启动后更新

更新了Logcat日志:

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);
   }
}

2 个答案:

答案 0 :(得分:1)

创建一个将保存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();
    }
}

答案 1 :(得分: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);
   }
}