android 开发中怎么设置闹铃和获取当前时间
展开全部
设置闹钟与获得当前时间,下面代码中都用
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TextView tv_text;
private Button btn_set;
private Button btn_close;
private Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = (TextView) findViewById(R.id.tv_text);
btn_set = (Button) findViewById(R.id.btn_set);
btn_close = (Button) findViewById(R.id.btn_close);
calendar = Calendar.getInstance();
/**设置闹钟*/
btn_set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//设置当前时间
calendar.setTimeInMillis(System.currentTimeMillis());
//获取小时
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//获取分钟
int minute = calendar.get(Calendar.MINUTE);
/**时间对话框*/
new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//设置当前时间
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);//设置小时
calendar.set(Calendar.MINUTE, minute); //设置分钟
calendar.set(Calendar.SECOND, 0); //设置秒数
calendar.set(Calendar.MILLISECOND, 0); //设置毫秒
/**建立Intent和PendingIntent,来调用目标组件*/
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
/**获取闹钟管理的实例*/
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
/**设置闹钟*/
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
/**设置周期闹钟*/
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(10*1000), (24*60*60*1000), pendingIntent);
String tmps = "设置闹钟时间为"+format(hourOfDay)+":"+format(minute);
tv_text.setText(tmps);
}
}, hour, minute, true).show();
}
});
/**取消闹钟*/
btn_close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);
tv_text.setText("闹钟已取消...");
}
});
}
/**格式转换:7:3--->07:03 */
private String format(int x){
String s=""+x;
if(s.length() ==1){
s="0"+s;
}
return s;
}
}
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TextView tv_text;
private Button btn_set;
private Button btn_close;
private Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = (TextView) findViewById(R.id.tv_text);
btn_set = (Button) findViewById(R.id.btn_set);
btn_close = (Button) findViewById(R.id.btn_close);
calendar = Calendar.getInstance();
/**设置闹钟*/
btn_set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//设置当前时间
calendar.setTimeInMillis(System.currentTimeMillis());
//获取小时
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//获取分钟
int minute = calendar.get(Calendar.MINUTE);
/**时间对话框*/
new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//设置当前时间
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);//设置小时
calendar.set(Calendar.MINUTE, minute); //设置分钟
calendar.set(Calendar.SECOND, 0); //设置秒数
calendar.set(Calendar.MILLISECOND, 0); //设置毫秒
/**建立Intent和PendingIntent,来调用目标组件*/
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
/**获取闹钟管理的实例*/
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
/**设置闹钟*/
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
/**设置周期闹钟*/
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(10*1000), (24*60*60*1000), pendingIntent);
String tmps = "设置闹钟时间为"+format(hourOfDay)+":"+format(minute);
tv_text.setText(tmps);
}
}, hour, minute, true).show();
}
});
/**取消闹钟*/
btn_close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);
tv_text.setText("闹钟已取消...");
}
});
}
/**格式转换:7:3--->07:03 */
private String format(int x){
String s=""+x;
if(s.length() ==1){
s="0"+s;
}
return s;
}
}
展开全部
更多追问追答
追问
谢谢,不过这不是我想要的
追答
噢 你想要干嘛?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询