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

전화올 때 서비스를 이용한 음성인식 질문

0 추천

안녕하세요 고수님들 ㅜㅜ

안드로이드 이제 막 시작한 학생입니다

핸드폰에 전화가 올 때 음성으로 전화를 받는 어플을 제작중인데

전화가 오면 전화 액티비티가 뜨다보니 서비스로 백그라운드에서 음성인식을 하게 하는 중입니다

main에서 phoneStateListener로 전화옴을 감지 한 후 intent를 VoiceRecognitionService로 보내어 처리하는데

VoiceRecognitionServicer객체가 create만 되고 더이상 작동하지 않음을 알게되었습니다.

무엇이 잘못되었는지 좀 알 수 있겠습니까? ㅜㅜ

 

동작은 "야"하면 텍스트뷰에 전화 받았다고 표시되고 (자동으로 받아지는건 향후 구현이요..)

"끊어"하면 텍스트뷰에 안 받았다고 표시됩니다

 

매니페스트에 서비스는 다음과 같이 등록하였고 아래는 소스 코드입니다

<service android:name="VoiceRecognitionService" android:enabled="true"></service>

 

- MainActivity.java

...중략

private PhoneStateListener phoneStateListener = new PhoneStateListener(){
		////////////////////////////////////////////////////리스너선언
		public void onCallStateChanged(int state, String incomingNumber){
    		super.onCallStateChanged(state, incomingNumber);

			if(state==TelephonyManager.CALL_STATE_RINGING) {
				//myReceiver = new MusicIntentReceiver();
				if(HeadsetReceiver.isHeadset==0){
//이어폰 안꼽았을 때
	    			phoneName=getContactName(incomingNumber);
//주소록에서 이름을 가져옴
	    			if(phoneName.equals("")){
	    				phoneName=incomingNumber;
	    			}
	    			
	    			try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
	    			speakOut(phoneName);
	    			
	    			Intent bindIntent = new Intent(MainActivity.this, VoiceRecognitionService.class);
	    			startService(bindIntent);
	                
	                if(howCall==1) {
	                	ttt.setText("받아");
	                } else if(howCall==0){
	                	ttt.setText("안 받아");
	                }
	                stopService(bindIntent);
				}
    		}
    	}
    };

...중략

- VoiceRecognitionService

public class VoiceRecognitionService extends Service{
	
	private SpeechRecognizer mRecognizer;
	public static ArrayList<String> mResult;
	
	@Override
	public void onCreate() {
		super.onCreate();

		Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);			//음성인식 intent생성
		i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());	//데이터 설정
		i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko-KR");							//음성인식 언어 설정
		
		
		mRecognizer = SpeechRecognizer.createSpeechRecognizer(this);				//음성인식 객체
		mRecognizer.setRecognitionListener(listener);										//음성인식 리스너 등록
		mRecognizer.startListening(i);												//음성인식 시작
		Toast.makeText(getApplicationContext(), "인식", Toast.LENGTH_SHORT).show();
	}
	
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null){
	        //startVoiceRecognition(); 
			
	    }
		
	    return super.onStartCommand(intent, flags, startId);
    }

	//음성인식 리스너
	private RecognitionListener listener = new RecognitionListener() {

		//음성 인식 결과 받음
		@Override public void onResults(Bundle results) {

			Intent i = new Intent();			//결과 반환할 intent
			i.putExtras(results);				//결과 등록

			mResult=i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			
			if(mResult.contains("야")) {
            	MainActivity.howCall=1;
            } else if(mResult.contains("끊어")){
            	MainActivity.howCall=0;
            }
		}
	};

...중략

안녕훈 (120 포인트) 님이 2013년 9월 24일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...