2012-02-17 2 views
0

서비스가 활성화 될 때 왜 내게 messageboxdialog를 보여주지 않습니다.안드로이드 서비스 클래스

@Override 
public void onStart(Intent intent, int startid) { 
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onStart"); 
    player.start(); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Are you sure you want to exit?") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        AlarmService.this.onDestroy(); 
       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 


} 

사운드가 재생되고있는 토스트가 나타나고 있습니다. 왜 쇼 다이얼로그는 오지 않았습니까?

+0

서비스에서 대화 상자를 만들 수 없습니다. 이 게시물을 참조하십시오 : http://stackoverflow.com/questions/5126868/showing-an-ok-dialog-box-from-a-service-receiver-in-android – AedonEtLIRA

+0

내가 원하는 것은 제게 버튼을 보여줄 때입니다. 서비스가 멈추기 위해 실행 중입니다. 그렇다면 서비스에서 Intent를 작성하고 onDestroy() 메소드가 서비스를 중지시키는 다른 Activity를 호출해야한다고 생각합니까? – Samuel

+0

활동이 전혀 필요하지 않습니다. 이 서비스에는 ** stopService (Intent) ** 메소드가 있습니다. 그냥 부르세요. http://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29 – AedonEtLIRA

답변

3

대화 상자에 AlertDialog을 작성하려면 builder.create()을 호출 한 다음 show()을 호출해야 표시 할 수 있습니다.

그러나 서비스 인 경우 직접 대화 상자를 표시 할 수 없습니다. 서비스 내에서 대화 상자를 표시하는 방법에 대한이 질문에 보라 : Alert dialog from Android service

+0

show()를 호출하면됩니다. Create가 자동으로 호출됩니다. – AedonEtLIRA

+0

어디에서 어떻게? – Samuel

+0

builder.show(); 그리고 언제 어디에서 보여주고 싶습니까? – AedonEtLIRA

0

그런 다음 show()의 호출에 의해 표시 할 수있는에 AlertDialog를 생성하기 위해 빌더에 create()를 호출해야합니다. 이 주제에 대한 자세한 내용은 the dialogues guide을 참조하십시오.