日期和时间用手机检查日期和时间
date and time checking with phones date and time
我正在尝试创建一个应用程序来检查用户提供的日期和时间与 phone 的日期和时间并执行特定任务。我可以使用日期和时间选择器对话框进行输入,但我遇到了如何处理它们的问题。
public class MainActivity extends Activity {
int SystemsTimeHour;
int SystemsTimeMin;
int hour ;
int min;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TimePickerDialog tpd = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour = hourOfDay;
min = minute;
}
}, hour, min, true);
tpd.show();
}
});
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
alarmIntent);
}
}
像这样创建一个日历对象:
Calendar rightNow = Calendar.getInstance()
这个对象是基于设备系统设置的,这正是你想要的。现在,获取时间:
Calendar.get(Calendar.HOUR_OF_DAY);
或者你可以使用这个:
public final Date getTime ()
Added in API level 1
Returns the time of this Calendar as a Date object.
Throws
IllegalArgumentException if the time is not set and the time cannot be >computed from the current field values.
来自 android 文档:
http://developer.android.com/reference/java/util/Calendar.html#getTime%28%29
获得系统时间后,要将其与您的时间进行比较,请执行以下操作:
if(myTime == SystemsTime){
//Do this
}else{
//Do this
}
您可以将 SystemsTime
设置为 Calendar.get(Calendar.HOUR_OF_DAY);
或 Calendar.getTime()
。
int SystemsTime = Calendar.get(Calendar.HOUR_OF_DAY);
您可以使用 AlarmManager 在特定时间触发 PendingIntent。
例如,如果您想在 5.30 起床,请使用这个时间创建一个日历实例。
int hour = 5;
int minute = 30;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR,hour);
calendar.set(Calendar.MINUTE,minute);
calendar.set(Calendar.SECOND, 0);
然后创建一个PendingIntent
Intent intent = new Intent(your_context,AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(your_context, 0,intent, 0);
然后使用AlarmManger对象设置在指定时间唤醒PendingIntent
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), alarmIntent);
您可以在广播接收器中实现您的功能。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "Alarm!!", Toast.LENGTH_LONG).show();
}
}
在清单中注册您的接收器
<receiver
android:name="yourpackagename.AlarmReceiver"
android:process=":remote" >
</receiver>
我正在尝试创建一个应用程序来检查用户提供的日期和时间与 phone 的日期和时间并执行特定任务。我可以使用日期和时间选择器对话框进行输入,但我遇到了如何处理它们的问题。
public class MainActivity extends Activity {
int SystemsTimeHour;
int SystemsTimeMin;
int hour ;
int min;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TimePickerDialog tpd = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour = hourOfDay;
min = minute;
}
}, hour, min, true);
tpd.show();
}
});
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
alarmIntent);
}
}
像这样创建一个日历对象:
Calendar rightNow = Calendar.getInstance()
这个对象是基于设备系统设置的,这正是你想要的。现在,获取时间:
Calendar.get(Calendar.HOUR_OF_DAY);
或者你可以使用这个:
public final Date getTime () Added in API level 1
Returns the time of this Calendar as a Date object. Throws IllegalArgumentException if the time is not set and the time cannot be >computed from the current field values.
来自 android 文档:
http://developer.android.com/reference/java/util/Calendar.html#getTime%28%29
获得系统时间后,要将其与您的时间进行比较,请执行以下操作:
if(myTime == SystemsTime){
//Do this
}else{
//Do this
}
您可以将 SystemsTime
设置为 Calendar.get(Calendar.HOUR_OF_DAY);
或 Calendar.getTime()
。
int SystemsTime = Calendar.get(Calendar.HOUR_OF_DAY);
您可以使用 AlarmManager 在特定时间触发 PendingIntent。
例如,如果您想在 5.30 起床,请使用这个时间创建一个日历实例。
int hour = 5;
int minute = 30;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR,hour);
calendar.set(Calendar.MINUTE,minute);
calendar.set(Calendar.SECOND, 0);
然后创建一个PendingIntent
Intent intent = new Intent(your_context,AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(your_context, 0,intent, 0);
然后使用AlarmManger对象设置在指定时间唤醒PendingIntent
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), alarmIntent);
您可以在广播接收器中实现您的功能。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "Alarm!!", Toast.LENGTH_LONG).show();
}
}
在清单中注册您的接收器
<receiver
android:name="yourpackagename.AlarmReceiver"
android:process=":remote" >
</receiver>