英文:
How can I access methods from my Main Activity in my Settings activity?
问题
我目前正在为 Android 开发一个语音录制应用程序。我正尝试从我的设置活动中访问 MainActivity
中的几个方法,以便为我的 MediaRecorder
更改一些设置。
我在 MainActivity
中有以下方法,用于设置录制的音频设置。
// 设置所有音频设置
private void setAudioSettings() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setAudioEncodingBitRate(96000);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
}
在我的设置活动中,我有一个标准的首选项界面,我希望在其中显示更改 MainActivity
中的媒体录制器的音频编解码器、采样率等选项。
在这里,我如何访问来自 MainActivity
的 setAudioSettings
方法,以便这样做?
如果您需要查看更多代码或屏幕截图,请告诉我。
英文:
I am currently developing a Voice Recorder app for Android. I am trying to access a few methods in my MainActivity
from my Settings activity, in order to change some settings for my MediaRecorder
.
I have the method below, which sets up the Audio Settings for the recording, in my MainActivity
.
// set up all audio settings
private void setAudioSettings() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setAudioEncodingBitRate(96000);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
}
In my Settings activity, I have a standard preferences screen that I would like to show options to change the audio codec, sampling rate, etc. of the media recorder in MainActivity
.
How can I access the setAudioSettings
method from MainActivity
here in order to do so?
If you need to see more code or screenshots, please let me know.
答案1
得分: 1
将该方法修改为静态方法,这样您可以在不创建类对象的情况下调用它。
public static void yourMethod(){
//在这里编写您的代码
}
并像这样调用您的方法:
MainActivity.yourMethod();
英文:
Make that method as static so you can call without creating the class object
public static void yourMethod(){
//Write your code here
}
And call your method like this way:
MainActivity.yourMethod();
答案2
得分: 1
简短回答是您不应该将一个活动的函数用于另一个活动。
对于您的情况,我建议您创建一个单例对象或共享偏好设置来存储设置屏幕数据。然后在MainActivity的onStart方法中,读取单例对象或共享偏好设置,并相应地调用#setAudioSettings
方法。
英文:
The short answer is you should not use the functions of your one activity into another activity.
For your case, I would suggest you to have a singleton object or shared preference to store your data of settings screen. Then in onStart of MainActivity, read the singleton object or shared preference and call #setAudioSettings
method accordingly.
答案3
得分: 0
保存设置,即在共享偏好中保存值,然后在主活动中从偏好中获取。
英文:
save setting i.e values in shared preferences and then get from preferences in Main Activity.
答案4
得分: 0
你可以通过以下方式将你的方法设为static
:
public static void setAudioSettings() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setAudioEncodingBitRate(96000);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
}
但为了做到这一点,mediaRecorder
也需要是静态的。
稍后,你可以从任何活动中调用此方法:
MainActivity.setAudioSettings();
你可以在此处了解有关static
关键字的更多信息,例如这里。
但是,我不能确定使用静态方法是否是解决你问题的最佳方案。也许在你的SettingActivity
中设置SharedPreferences
,然后在你的MainActivity
的onResume()
方法中调用setAudioSettings()
方法,并从SharedPreferences
中获取这些值会更好?
英文:
You can make your method static
by:
public static void setAudioSettings() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setAudioEncodingBitRate(96000);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
}
But to do that mediaRecorder
needs to be static also.
Later you can call this method from any activity by:
MainActivity.setAudioSettings();
You can learn more about static
keyword for example here.
But, I am not sure that use of static method is the best solution for exactly your problem, maybe will be better to set SharedPreferences
in your SettingActivity
and later in onResume()
of your MainActivity
call setAudioSettings()
method and get there values from SharedPreferences
?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论