android中闹钟程序是怎样监听系统时间的 100
刚学android不久,想了解下,闹钟是怎样监听系统时间,到点就闹?能详细点最好,谢谢!三楼问得好,楼下的大哥,它总不能每时每刻都在判断tm是不是你定的闹钟时间吧?初学者...
刚学android不久,想了解下,闹钟是怎样监听系统时间,到点就闹?能详细点最好,谢谢!
三楼问得好,
楼下的大哥,它总不能每时每刻都在判断tm 是不是 你定的闹钟时间吧?
初学者,问得不好请见谅。 展开
三楼问得好,
楼下的大哥,它总不能每时每刻都在判断tm 是不是 你定的闹钟时间吧?
初学者,问得不好请见谅。 展开
5个回答
展开全部
Using AlarmManager. here is the implementation for DeskClock, which is a Android original application.
Firstly you should set your alarm into AlarmManager with pending intent.
1. Get AlarmManager:
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
2. Set your time as long format into AlarmManager instance with pending intent.
Intent intent = new Intent(ALARM_ALERT_ACTION);
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
Now when the system time gets to atTimeInMillis, AlarmManager will send a broadcast.
Secondly Register a BroadcastReceiver to receive the broadcast and do what you want in onReceive method.
For more information, please reference the source code on desk-clock, which is under android source code/packages/app/desk-clock.
Any help, please feel free to let me know.
Firstly you should set your alarm into AlarmManager with pending intent.
1. Get AlarmManager:
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
2. Set your time as long format into AlarmManager instance with pending intent.
Intent intent = new Intent(ALARM_ALERT_ACTION);
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
Now when the system time gets to atTimeInMillis, AlarmManager will send a broadcast.
Secondly Register a BroadcastReceiver to receive the broadcast and do what you want in onReceive method.
For more information, please reference the source code on desk-clock, which is under android source code/packages/app/desk-clock.
Any help, please feel free to let me know.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置闹钟与获得当前时间,下面代码中都用
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是的
关机之后就不起作用,需要每次开机后重新设置下
查看原帖>>
关机之后就不起作用,需要每次开机后重新设置下
查看原帖>>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-06-09
展开全部
SL,它总不能每时每刻都在判断tm 是不是 你定的闹钟时间吧?
初学者,问得不好请见谅。
初学者,问得不好请见谅。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询