如何在特定时间和日期开始闹钟,我使用TimePicker
和DatePicker
获得时间和日期。即使我可以在用户指定的时间启动闹钟,但我无法在用户指定的日期执行此操作。我怎样才能达到目标?
我已经完成的代码。
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;
}
}
例如: 我想在05-04-2013 12:00 pm设置闹钟..我怎样才能实现?
答案 0 :(得分:1)
以下是我从现在起5分钟后设置退出警报的方法
Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.MINUTE, 5);
您可以使用 Calendar 中提供的方法和字段对日期执行更多操作。例如:
Calendar cal = Calendar.getInstanc();
cal.set(Calendar.MONTH, JANUARY);
设置月份。检查该链接,您可以看到可以设置它的所有不同方法。使用set()
设置字段(月,日等)并使用add()
添加到字段中。这应该可以帮助你开始
修改强>
无论您在何处设置待定意图,只需使用已设置为值的变量
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);
}
然后点击Button
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);
}});
如果你已经在设定时间,那么你知道怎么做。希望这可以帮助你
答案 1 :(得分: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();
}
答案 2 :(得分:0)
您可以在此链接中找到您的主题: 的 How to start an immediate alarm on Android 强>
谢谢