2011-01-27 3 views
1

내 앱에 일부 파일이 있지만 그 중 3 개만 중요합니다. 알림 음 및 알림이 포함 된 미리 알림 앱입니다. 체크 상자와 리스너를 포함하는 maincode.java 파일이 있습니다. 사용자가 Chechbox를 체크인하면 AlarmManager는 MyService.java를 시작하는 AlarmReceiver.java로 인 텐트를 보냅니다. MyService java에는 사운드 재생에 대한 코드가 들어 있습니다. 코드는 부분적입니다. MyService.java :Android AlarmManager 및 서비스 질문

public void onCreate() { 
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onCreate"); 

    player = MediaPlayer.create(this, R.raw.sound); 
    player.setLooping(false); // Set looping 
} 

@Override 
public void onDestroy() { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 
    player.stop(); 
} 

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

AlarmReceiver.java : maincode.java의

public void onCreate() { 
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onCreate"); 

    player = MediaPlayer.create(this, R.raw.sound); 
    player.setLooping(false); // Set looping 

중요한 부분은 :

cb1 = (CheckBox) findViewById(R.id.CheckBox01); 
    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     {   
      if (cb1.isChecked()) 
       { 
       if (GlobalVars.getHourOfDay() >= 0) 
       { 
        Toast.makeText(maincode.this, "ok", Toast.LENGTH_SHORT).show(); 
        rem1.setText(GlobalVars.getReminder1name()); 
         Intent intent = new Intent(maincode.this, AlarmReceiver.class); 
         PendingIntent pendingIntent = PendingIntent.getBroadcast(bInsulinReminder.this, 0, 
          intent, PendingIntent.FLAG_UPDATE_CURRENT); 
         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
         Calendar cal = Calendar.getInstance(); 
         cal.set(Calendar.HOUR_OF_DAY, GlobalVars.getHourOfDay()); 
         cal.set(Calendar.MINUTE, GlobalVars.getMinute()); 
         cal.set(Calendar.SECOND, 0); 
         cal.set(Calendar.MILLISECOND, 0); 
         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+ 3000, 6000, pendingIntent); 

       } 
       Toast.makeText(maincode.this, "Checked", Toast.LENGTH_SHORT).show(); 
       } else { 
        rem1.setText("No reminder set"); 
        Toast.makeText(maincode.this, "Not checked", Toast.LENGTH_SHORT).show(); 
       } 
     } 

     }); 

(REM1는 그 텍스트의 이름에 따라 알림 버튼입니다 사용자가 원하는 모든 것)

코드의 문제점은 내가 시작하면 알람, 멈출 수 없어. MyService.java에 player.stop() 명령이 있다는 것을 알고 있습니다 만, 체크 박스가 선택되지 않은 maincode.java의 끝에서 어떻게 호출 할 수 있습니까?

답변

3

아니요. 리스너에서 직접 할 수 없습니다. 당신은 알람이 방법으로 해제 할 수 있습니다

Intent intent = new Intent(maincode.this, AlarmReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(bInsulinReminder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
pendingItem.cancel(); 
alarmManager.cancel(pendingItem); 

을 또는 (내 생각)하는 경우 AlarmReceiver는 브로드 캐스트 리시버의 구현이며, onReceive 방법에서 당신은 서비스 클래스의 구현입니다 귀하의이면 MyService를 시작합니다.

maincode.java 리스너 내부에서이 알람을 중지하려면 AlarmReceiver에서 사용한 PendingIntent를 다시 작성하고 stopService 메소드를 실행하여 MyService를 중지하면됩니다.

희망이 있습니다.