gpt4 book ai didi

java - 从 android 中的另一个 Activity 调用函数

转载 作者:行者123 更新时间:2023-12-01 12:22:30 24 4
gpt4 key购买 nike

大家好,我是 Android 开发新手,我正在关注 THIS tutorial制作一个简单的音频管理器。但我希望某些函数位于另一个 Activity 类上,并根据相应的按钮按下来调用这些函数。其功能是:

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。您的帮助对我来说将是一种荣幸。交易。

发布后更新

更新了 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);
}
}

最佳答案

创建一个包含 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();
}
}

关于java - 从 android 中的另一个 Activity 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26573913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com