如何更新闹钟时间

时间:2014-11-04 10:12:10

标签: android-alarms

如何更新时间更新时的闹钟时间? 在我的代码警报响铃两次,同时更新时间如何更新时间?     公共类DiaryAddRem_SPORTS extends Activity实现OnClickListener {

EditText sports_title_et;
TextView sports_reminder_et,sports_date_et,sports_time_et;
Button sports_save,sports_cancel;
SQLREM_SPORTS_Controller dbcon;


static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID=1;
public  int year,month,day,hour,minute;  
private int mYear, mMonth, mDay,mHour,mMinute; 

public DiaryAddRem_SPORTS()
{

            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);
}




String text;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.diary_rem_sports);

    sports_title_et = (EditText) findViewById(R.id.sports_title);
    sports_date_et = (TextView) findViewById(R.id.sports_date);
    sports_time_et = (TextView) findViewById(R.id.sports_time);

    sports_date_et.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

             showDialog(DATE_DIALOG_ID);
        }
    });



    sports_time_et.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

             showDialog(TIME_DIALOG_ID);
        }
    });


    sports_save = (Button) findViewById(R.id.sports_save);
    sports_cancel = (Button) findViewById(R.id.sports_cancel);

    sports_cancel.setOnClickListener(new View.OnClickListener() {                                            
        public void onClick(View v) 
          {             
    Intent launchactivity= new Intent(DiaryAddRem_SPORTS.this,DiaryRemList_SPORTS.class);                               
    startActivity(launchactivity);                          
            }
             });  



    dbcon = new SQLREM_SPORTS_Controller(this);
    dbcon.open();


    sports_save.setOnClickListener(this);

}

private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int yearSelected,
                                  int monthOfYear, int dayOfMonth) {
               year = yearSelected;
               month = monthOfYear;
               day = dayOfMonth;

               sports_date_et.setText(day+"-"+month+"-"+year);
            }
        };


        private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {

                public void onTimeSet(TimePicker view, int hourOfDay, int min) {
                    hour = hourOfDay;
                    minute = min;

                    sports_time_et.setText(hour+":"+minute);
                  }
            };



        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_DIALOG_ID:

                return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, mDay);

            case TIME_DIALOG_ID:
                return new TimePickerDialog(this,
                        mTimeSetListener, mHour, mMinute, false);

            }
            return null;
        }




public void onBackPressed() {


      Intent launchactivity= new Intent(DiaryAddRem_SPORTS.this,DiaryRemList_SPORTS.class);                               
        startActivity(launchactivity);                          

}



public void onClick(View v){


    switch (v.getId()) {

    case R.id.sports_save:

        String sports_title      = sports_title_et.getText().toString();
        String sports_date       = sports_date_et.getText().toString();
        String sports_time   = sports_time_et.getText().toString();


        Intent i = new Intent("com.appsinbox.mydiary.remainder.demoactivity");
        PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
        AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
        GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
        long alarm_time = calendar.getTimeInMillis();
        alarmManager.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);
        Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show();


        if(sports_title.equals("")||sports_date.equals("")||sports_time.equals(""))
        {
            Toast.makeText(getApplicationContext(), "Please enter mandatory fields", Toast.LENGTH_LONG).show();
            return; 
        }
        else
        {
            dbcon.insertData(sports_title, sports_date, sports_time);
            Intent sports = new Intent(DiaryAddRem_SPORTS.this, DiaryRemList_SPORTS.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(sports);
        }


        break;

    default:
        break;
    }

}}

AlertDemo.java

public class AlertDemo extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    /** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
    getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_DISMISS_KEYGUARD);


    /** Creating a alert dialog builder */
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    /** Setting title for the alert dialog */
    builder.setTitle("Alarm");

    /** Setting the content for the alert dialog */
    builder.setMessage("An Alarm by AlarmManager");

    /** Defining an OK button event listener */
    builder.setPositiveButton("OK", new OnClickListener() { 
        @Override
        public void onClick(DialogInterface dialog, int which) {
            /** Exit application on click OK */
            getActivity().finish();
        }                       
    });
    final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.sounds);
    mp.start();
    /** Creating the alert dialog window */
    return builder.create();
}

/** The application should be exit, if the user presses the back button */ 
@Override
public void onDestroy() {       
    super.onDestroy();
    getActivity().finish();
}}

DemoActivity

public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState); 

    /** Creating an Alert Dialog Window */
    AlertDemo alert = new AlertDemo();

    /** Opening the Alert Dialog Window */
    alert.show(getSupportFragmentManager(), "AlertDemo");       
}}

0 个答案:

没有答案