2014-01-16 4 views
0

* 안녕하세요! 사용자 지정 DialogPreference 사용하고 있지만 사용자가 시간을 설정할 때 서비스를 시작하려면 .. alrmmanager 작동하도록 .. 즉, onTimeSet() 메서드에서 내 코드를 입력하면 거기에 경보 서비스를 추가 할 수 없습니다 .. 그래서 사람이이 가능 얼마나 도움이 될 수 있습니다 *DialogPreference에서 Alram t Start Service를 설정하는 방법

당신은 그래서를 만들 알람 매니저의 인스턴스를 생성 한 후 일정 시간을 기준으로합니다 서비스 클래스에 대한 의도와 보류중인 의도를 만들 필요가
public class Timer extends DialogPreference { 
    protected int lastHour=0; 
    protected int lastMinute=0; 
    protected boolean is24HourFormat; 
    protected TimePicker picker=null; 
    protected TextView timeDisplay; 

    DatagramPacket send; 
    DatagramSocket d1; 
    InetAddress ip; 

    public Timer(Context ctxt) { 
     this(ctxt, null); 
    } 

    public Timer(Context ctxt, AttributeSet attrs) { 
     this(ctxt, attrs, 0); 
    } 

    public Timer(Context ctxt, AttributeSet attrs, int defStyle) { 
     super(ctxt, attrs, defStyle); 

     is24HourFormat = DateFormat.is24HourFormat(ctxt); 
     setPositiveButtonText("Set"); 
     setNegativeButtonText("Cancel"); 
    } 

    @Override 
    public String toString() { 
     if(is24HourFormat) { 
      return ((lastHour < 10) ? "0" : "") 
        + Integer.toString(lastHour) 
        + ":" + ((lastMinute < 10) ? "0" : "") 
        + Integer.toString(lastMinute); 
     } else { 
      int myHour = lastHour % 12; 
      return ((myHour == 0) ? "12" : ((myHour < 10) ? "0" : "") + Integer.toString(myHour)) 
        + ":" + ((lastMinute < 10) ? "0" : "") 
        + Integer.toString(lastMinute) 
        + ((lastHour >= 12) ? " PM" : " AM"); 
     } 
    } 

    @Override 
    protected View onCreateDialogView() { 
     picker=new TimePicker(getContext().getApplicationContext()); 
     return(picker); 
    } 

    @Override 
    protected void onBindDialogView(View v) { 
     super.onBindDialogView(v); 
     picker.setIs24HourView(is24HourFormat); 
     picker.setCurrentHour(lastHour); 
     picker.setCurrentMinute(lastMinute); 
    } 

    @Override 
    public void onBindView(View view) { 
     View widgetLayout; 
     int childCounter = 0; 
     do { 
      widgetLayout = ((ViewGroup) view).getChildAt(childCounter); 
      childCounter++; 
     } while (widgetLayout.getId() != android.R.id.widget_frame); 
     ((ViewGroup) widgetLayout).removeAllViews(); 
     timeDisplay = new TextView(widgetLayout.getContext()); 
     timeDisplay.setText(toString()); 
     ((ViewGroup) widgetLayout).addView(timeDisplay); 
     super.onBindView(view); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
     super.onDialogClosed(positiveResult); 

     if (positiveResult) { 
      picker.clearFocus(); 
      lastHour=picker.getCurrentHour(); 
      lastMinute=picker.getCurrentMinute(); 

      String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute); 

      if (callChangeListener(time)) { 
       persistString(time); 
       timeDisplay.setText(toString()); 




      } 
     } 
    } 

    @Override 
    protected Object onGetDefaultValue(TypedArray a, int index) { 
     return(a.getString(index)); 
    } 

    @Override 
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { 
     String time=null; 

     if (restoreValue) { 
      if (defaultValue==null) { 
       time=getPersistedString("00:00"); 
      } 
      else { 
       time=getPersistedString(defaultValue.toString()); 
      } 
     } 
     else { 
      if (defaultValue==null) { 
       time="00:00"; 
      } 
      else { 
       time=defaultValue.toString(); 
      } 
      if (shouldPersist()) { 
       persistString(time); 
      } 
     } 

     String[] timeParts=time.split(":"); 
     lastHour=Integer.parseInt(timeParts[0]); 
     //lastMinute=Integer.parseInt(timeParts[1]); 
    } 

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     // Do something with the time chosen by the user 




    } 


} 

답변

0

캘린더의 인스턴스를 지정하고 사용자가 설정 한 시간을 할당하려면 Alar Manager를 아래 코드와 같이 Calendar.getInstance()로 설정하십시오.

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(this, AlarmReceiver.class); 
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), alarmIntent); 
+0

위의 onTimeSet() 메서드에서 수행하고있는 작업이지만 위의 메서드는 언급하지 않았습니다.하지만 onTimeSet()의 getSystemService 메서드를 선언 할 수 없다는 오류가 발생합니다. – Raman