简体   繁体   中英

Passing data from activity to non-activity class without leaving current activity?

I have an tts non-UI class and activity class.Now in the activity class i have a button from which user can either either male or female voice depending on the selection i want to pass on the string to tts class and once tts class receives the string based on the string received it sets the float variable to particular value and that float variable is then used in setPitch() method of tts. So i was wondering if there is any way to pass on the string to this tts class.I know how to pass string between activities but here i want to pass data from my activity class to an non activity class without leaving my activity class.Thank You.

My TextSpeaker(texttospeech) class is as follows: public class TextSpeaker implements OnInitListener{

TextToSpeech tts;
public TextSpeaker(Context c){
    tts = new TextToSpeech(c,this);
}

@Override
public void onInit(int status) {
    if(status != TextToSpeech.ERROR){
        tts.setLanguage(Locale.US);
tts.setPitch(1);
    }

}
}

The easiest solution will be to make a Global class and define all the variables which you want to share between different activities and classes there as 'static'. Then simply use Global.varName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM