2013-04-03 75 views
2

如何在特定的時間和日期開始鬧鐘,我在哪裏得到時間和日期使用TimePickerDatePicker。即使我可以在用戶指定的時間開始鬧鐘,但我無法在用戶指定的日期進行鬧鐘。我怎樣才能達到我的目標? 我已經完成的代碼。如何在特定日期和時間啓動鬧鐘?

package com.example.modifiedalarm; 
import java.util.Calendar; 
import java.util.Locale; 
import java.util.TimeZone; 
import android.os.Bundle; 
import android.provider.AlarmClock; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import android.widget.Toast; 
import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.DatePickerDialog; 
import android.app.DatePickerDialog.OnDateSetListener; 
import android.app.PendingIntent; 
import android.app.TimePickerDialog; 
import android.app.TimePickerDialog.OnTimeSetListener; 
import android.content.Context; 
import android.content.Intent; 

public class MainActivity extends Activity { 
    Button b1, b2, b3; 
    Calendar c; 
    String abcd; 
    TextView ed, tv; 
    int hr, min, year, month, date; 
    int time = 0; 
    int daa = 0; 

private PendingIntent pendingIntent; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     b1 = (Button) findViewById(R.id.button1); 
     b2 = (Button) findViewById(R.id.button2); 
     b3 = (Button) findViewById(R.id.button3); 
     ed = (TextView) findViewById(R.id.settime); 
     tv = (TextView) findViewById(R.id.gettime); 
     c = Calendar.getInstance(); 

     hr = c.get(Calendar.HOUR_OF_DAY); 
     min = c.get(Calendar.MINUTE); 
     year = c.get(Calendar.YEAR); 
     month = c.get(Calendar.MONTH); 
     date = c.get(Calendar.DAY_OF_MONTH); 

     b2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(AlarmClock.ACTION_SET_ALARM); 
      i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm"); 
      i.putExtra(AlarmClock.EXTRA_HOUR, hr); 
      i.putExtra(AlarmClock.EXTRA_MINUTES, min); 
      startActivity(i); 
}});     

     b1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       // showDialog(time); 
       new TimePickerDialog(MainActivity.this, timeval, hr, min, true) 
         .show(); 

      } 

     }); 

     b3.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       new DatePickerDialog(MainActivity.this, dateval, year, month, 
         date).show(); 
      } 
     }); 
    } 


    OnDateSetListener dateval = new OnDateSetListener() { 

     @Override 
     public void onDateSet(DatePicker view, int Year, int monthOfYear, 
       int dayOfMonth) { 
      // TODO Auto-generated method stub 
      year = Year; 
      month = monthOfYear; 
      date = dayOfMonth; 
      updatedate(); 
     } 
    }; 


    OnTimeSetListener timeval = new OnTimeSetListener() { 
     @Override 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      // TODO Auto-generated method stub 
      hr = hourOfDay; 
      min = minute; 
      updatetime(); 
     } 
    }; 

    public void updatedate() { 
     tv.setText(new StringBuilder().append(date).append("/").append(month).append("/").append(year)); 
     String og=tv.getText().toString(); 
     //int ogx=Integer.parseInt(og); 
     Toast.makeText(MainActivity.this, "the date is "+og, Toast.LENGTH_LONG).show(); 
     datecompare(); 

} private void datecompare() { 

    } 
private void updatetime() { 
     // TODO Auto-generated method stub 
     ed.setText(new StringBuilder().append(hr).append(":").append(min)); 
     String b=hr + ":" + min; 
     } 

} 

例如: 我想設置鬧鐘在2013年5月4日下午12:00 ..我怎樣才能做到這一點?

+0

使用'Calendar()方法''Calendar'實例。告訴我們你有什麼,所以我們可以幫助 – codeMagic

+0

@codeMagic如果我得到日曆實例它會得到設備時間正確嗎? ?所以用這個設備時間我不能做任何事情,除了顯示。 –

+0

不,您可以使用'日曆'API – codeMagic

回答

1

這是我如何設置註銷報警5分鐘從現在

Calendar cal1 = Calendar.getInstance(); 
cal1.add(Calendar.MINUTE, 5); 

你可以做使用Calendar中可用的方法和字段的日期更多。例如:

Calendar cal = Calendar.getInstanc(); 
cal.set(Calendar.MONTH, JANUARY); 

設置月份。檢查鏈接,你可以看到你可以設置它的所有不同的方式。使用set()來設置字段(月,日等),並使用add()添加到字段中。這應該可以幫助您無論如何

開始編輯

無論你設置當您點擊Button

待處理的意圖只是使用您已設置爲值

private void updatedate() { 
    // TODO Auto-generated method stub 
    c.set(Calendar.Month, month);   // `c` is the `Calendar` instance you defined earlier. Now we are setting the month on that instance 
    c.set(Calendar.DAY_OF_MONTH, date); 
    } 

然後這些變量

b2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     long alarmTime = cal.getTimeInMillis(); // convert your calendar instance with the dates set to millis 
      Intent i = new Intent(AlarmClock.ACTION_SET_ALARM); 
     i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm"); 
     i.putExtra("alarmTime", alarmTime);  //"alarmTime" will be used to get the time to set the alarm in your AlarmClock class 
     startActivity(i); 
}}); 

其餘的你知道如何做,如果你已經設定了時間。希望這對你有所幫助

+0

沒有這個不是在尋找 –

+0

那麼你在找什麼?這是你如何設定一個特定的日期。查看我的編輯 – codeMagic

+0

親愛的@CodeMagic ..我已經這樣做了,你看到我的代碼.. ?? –

1
AlarmManager: 
    Calendar cal = Calendar.getInstance(); 

      long when =0; 
      intent = new Intent(this, AlarmReceiver.class); 
      intent.putExtra("eventName", eventName); 
      intent.putExtra("eventDescription",eventDescription); 

      // Get the AlarmManager service 
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 

      RadioGroup rg=(RadioGroup)findViewById(R.id.Setting_rgRepeatMode); 
      selectedRadio=(RadioButton)findViewById(rg.getCheckedRadioButtonId()); 
      long repeatTime=0; 

       cal.set(mYear,mMonth,mDay,mHour,mMinute,0); 
       intent.putExtra("Flag",true); 
       PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
       when=cal.getTimeInMillis(); 
       am.set(AlarmManager.RTC_WAKEUP,when, pendingIntent); 
       return; 

    BroadCastReceiver: 

     public class MyBroadcastReceiver extends BroadcastReceiver { 
     try { 
//Here you can write your logic 
        Toast.makeText(context,bundle.getString("eventName"), Toast.LENGTH_SHORT).show(); 

      NotificationManager notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
      int icon = R.drawable.event; 
      CharSequence notiText = "Event Notification"; 
      long time = System.currentTimeMillis(); 
      @SuppressWarnings("deprecation") 
      Notification notification = new Notification(icon, notiText,time); 
      notification.defaults |= Notification.DEFAULT_SOUND; 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      Intent notificationIntent = new Intent(context, Setting.class); 
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
      notification.setLatestEventInfo(context,intent.getStringExtra("eventName"),intent.getStringExtra("eventDescription"), contentIntent); 
      int SERVER_DATA_RECEIVED = 1; 
      Calendar cal=Calendar.getInstance(); 
      Toast.makeText(context,"aavechhe "+intent.getBooleanExtra("Flag",false),Toast.LENGTH_SHORT).show(); 
      if(intent.getIntExtra("month",0)==cal.get(Calendar.DAY_OF_MONTH)) 
      { 
       Toast.makeText(context,"aavechhe "+cal.get(Calendar.DAY_OF_MONTH),Toast.LENGTH_SHORT).show(); 
       notificationManager.notify(SERVER_DATA_RECEIVED, notification); 
      } 
      if(intent.getBooleanExtra("Flag",false)) 
       notificationManager.notify(SERVER_DATA_RECEIVED, notification); 

     } catch (Exception e) { 
      Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 

     } 
+0

這裏我在特定時間顯示通知。但您可以編寫報警代碼。 –

相關問題