마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

알람매니저를 통해 2초뒤에 TTS가 나오도록 소스를 구현중인데요

0 추천
 
// MainActivity 소스이고 
public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
    public TextView textView;
    public TextToSpeech tts;
    @Override
    public void onInit(int status){

        if(status== TextToSpeech.SUCCESS){
            tts.setLanguage(Locale.KOREAN);//한국말 설정
            tts.setPitch(0.5f);
            tts.setSpeechRate(1.0f);

        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        textView = (TextView)findViewById(R.id.textView);
        String str = ("아리리리");
        textView.setText(str);

        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);

        // 알람을 받을 시간을 5초 뒤로 설정
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 2);

        // 알람 매니저에 알람을 등록
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);





    }
    public String test() {
        return textView.getText().toString();
    }
}
//알람매니저 소스입니다 
public class AlarmReceiver extends BroadcastReceiver implements TextToSpeech.OnInitListener {
    public MainActivity mainActivity;
    public TextToSpeech tts;
    public AlarmReceiver AlarmReceiver;

    @Override
    public void onInit(int status){
        if(status== TextToSpeech.SUCCESS){
            tts.setLanguage(Locale.KOREAN);//한국말 설정
            tts.setPitch(0.5f);
            tts.setSpeechRate(1.0f);



        }
    }


  /*  public void setAlarm()
    {
        alarmReceiver = new AlarmReceiver();

    }*/

    public void AlarmReceiver(MainActivity ma) {

        mainActivity=ma;
        tts = new TextToSpeech(mainActivity, this);

    }

    @Override
    public void onReceive(Context context, Intent intent) {
       // alarmReceiver.setAlarm();

        String str=mainActivity.test();
        Toast.makeText(context,"알람",Toast.LENGTH_SHORT).show();
        tts.speak(str,TextToSpeech.QUEUE_FLUSH, null, null);

    }
알람매니저를 통해서 2초뒤에 TTS가 나오도록 하는 소스인데 2초뒤에
어플이 중지되었다고 뜨네요 
onReceive 함수에서 toast 메세지 구문만 넣으면 오류가없이
2초뒤에 토스트메세지가 뜨는데 TTS를 넣으려니 자꾸 어플이 중지되었다고 뜨네요 ㅠㅠ  소스를 어떻게 수정해야하나요?ㅠㅠ
 
안드개발자777 (140 포인트) 님이 2017년 6월 10일 질문
어떻게 소스를 수정해야할까요 ㅠㅠ
onReceive 부분에 String str=mainActivity.test(); 로 접근하는것도 에러가나네요

1개의 답변

0 추천
@Override
    public void onReceive(Context context, Intent intent) {
       // alarmReceiver.setAlarm();
        this.MainActivity = (MainActivity)context;
        String str=mainActivity.test();
        Toast.makeText(context,"알람",Toast.LENGTH_SHORT).show();
        tts = new TextToSpeech(mainActivity, this);
        tts.speak(str,TextToSpeech.QUEUE_FLUSH, null, null);

    }
익명사용자 님이 2017년 6월 12일 답변
소스 수정을 하였는데도 불구하고 2초뒤에 오류가 뜨네요..
...