2013-05-30 1 views
1

서비스의 루프에서 실행되는 사운드 파일이 있습니다.루핑 사운드 파일로 서비스 종료

서비스를 시작하는 활동에는 단추가 있습니다. 버튼을 누르면 서비스의 사운드 파일이 재생을 멈추게합니다. 즉 서비스를 중지합니다. 지금까지 stopService()으로 전화를 걸면 서비스가 계속 실행됩니다.

어떤 아이디어? 감사.

활동

public class AlarmPage extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_alarm_page); 
    TextView timeview = (TextView)findViewById(R.id.timefiled); 
    final Button dismissButton =(Button)findViewById(R.id.dismissButton); 
    Bundle b = getIntent().getExtras(); 
    String hrval = b.getString("HR"); 
    String minval = b.getString("MN"); 
    timeview.setText(hrval + ":" + minval); 

    final Intent alarmring = new Intent(this, alarmringService.class); 

    startService(alarmring); 
    dismissButton.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 

      // TODO Auto-generated method stub 
      Intent alarmmenu = new Intent(getApplicationContext(),SetAlarmsActivity.class); 
      startActivity(alarmmenu); 

      System.out.println("Button:" + dismissButton.isPressed()); 
      stopService(alarmring); 

     } 

    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.alarm_page, menu); 
    return true; 
} 
} 

서비스

public class alarmringService extends IntentService{ 

public alarmringService(){ 
    super("alarmringService"); 
    // TODO Auto-generated constructor stub 

} 

@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 


    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.drawable.startrekcommsound); 
    mp.setLooping(true); 
    try { 
     mp.prepare(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    mp.start(); 
} 

} 

답변

0

나는 내가 전문가가 아니에요 때문에 작동하지 않습니다,하지만 수정 이유를 볼 수 있습니다 문제는 의도를 사용할 수 있습니다. 그건 그렇고, 더 이상 일이 없을 때 IntentService가 멈 춥니 다.

시험해보기 : IntentService 인 텐트에 자체를 중지하라는 명령을 보내주십시오. 그런 다음 onHandleIntent()에서 요청한 것이 맞는지 확인하고 에서 stopSelf()으로 전화하십시오.

또한 MediaPlayer의 작동 방식을 알지 못하지만 어쩌면 자신의 스레드가 있으며이 스레드도 중지해야합니다.