问题描述
我想建立一个个人应用程序,将在DeskClock应用程序设置警报。我可以得到它的当前日期设置报警任何时候,但我将如何去约在一周设定一个报警的第二天,或更高版本。翻翻AlarmClock的API在Android中我没有看到一个正常的方式来做到这一点。这甚至可能?
顺便说一下,这是我的code设定报警它可能不是pretty的,但我学习,我去。
包com.netwokz.setit;
进口的java.util.Calendar;
进口java.util.GregorianCalendar中;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.provider.AlarmClock;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
公共类MainActivity扩展活动实现OnClickListener {
按钮btnSetAlarm;
的EditText etHour,etMinute;
INT分钟,小时,日;
日历CAL;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main_activity);
btnSetAlarm =(按钮)findViewById(R.id.btn_set_alarm);
etHour =(EditText上)findViewById(R.id.etHour);
etMinute =(EditText上)findViewById(R.id.etMinute);
btnSetAlarm.setOnClickListener(本);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
。getMenuInflater()膨胀(R.menu.main_activity,菜单);
返回true;
}
@覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.btn_set_alarm:
setAlarm();
打破;
}
}
私人无效setAlarm(){
CAL =新的GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis的());
天= cal.get(Calendar.DAY_OF_WEEK);
小时= cal.get(Calendar.HOUR_OF_DAY);
分钟= cal.get(Calendar.MINUTE);
意图I =新的意图(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_HOUR,小时+的Integer.parseInt(etHour.getText()的toString())。);
i.putExtra(AlarmClock.EXTRA_MINUTES,分+的Integer.parseInt(etMinute.getText()的toString()));
i.putExtra(AlarmClock.EXTRA_SKIP_UI,真正的);
startActivity(ⅰ);
}
}
试试...这code。这code正在完善... enjoy..full code ...
MainActivity.java
公共类MainActivity延伸活动{
TimePicker myTimePicker;
按钮buttonstartSetDialog;
TextView的textAlarmPrompt;
TimePickerDialog timePickerDialog;
最终静态INT RQS_1 = 1;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
textAlarmPrompt =(TextView中)findViewById(R.id.alarmprompt);
buttonstartSetDialog =(按钮)findViewById(R.id.startAlaram);
buttonstartSetDialog.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
textAlarmPrompt.setText();
openTimePickerDialog(假);
}
});
}
私人无效openTimePickerDialog(布尔is24r){
台历挂历= Calendar.getInstance();
timePickerDialog =新TimePickerDialog(MainActivity.this,
onTimeSetListener,calendar.get(Calendar.HOUR_OF_DAY)
calendar.get(Calendar.MINUTE),is24r);
timePickerDialog.setTitle(设定的闹钟时间);
timePickerDialog.show();
}
OnTimeSetListener onTimeSetListener =新OnTimeSetListener(){
@覆盖
公共无效onTimeSet(TimePicker观点,诠释hourOfDay,INT分钟){
日历calNow = Calendar.getInstance();
日历calSet =(日历)calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY,hourOfDay);
calSet.set(Calendar.MINUTE,分钟);
calSet.set(Calendar.SECOND,0);
calSet.set(Calendar.MILLISECOND,0);
如果(calSet.compareTo(calNow)所述; = 0){
//今天通过设置时间,数到明天
calSet.add(Calendar.DATE,1);
}
setAlarm(calSet);
}
};
私人无效setAlarm(日历targetCal){
textAlarmPrompt.setText(\ñ\ñ*** \ N+报警设置
+ targetCal.getTime()+\ N+*** \ N);
意向意图=新的意图(getBaseContext(),AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(),RQS_1,意向,0);
AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),
pendingIntent);
}
}
Reciver.java
公共类AlarmReceiver扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(上下文K1,K2意向){
// TODO自动生成方法存根
Toast.makeText(K1,接到报警!,Toast.LENGTH_LONG).show();
}
}
main_activity.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:填充=10dp>
<按钮
机器人:ID =@ + ID / startAlaram
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=设置Alaram时间/>
<的TextView
机器人:ID =@ + ID / alarmprompt
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#000000/>
< / LinearLayout中>
的Manifest.xml
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=。MainActivity
机器人:标签=@字符串/ title_activity_main>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<接收机器人:AlarmReceiver名称=安卓程序=:远程/>
< /用途>
I'm trying to build a personal app that will set alarms in the DeskClock app. I can get it to set alarms for anytime in the current day, But how would I go about setting an alarm for the next day, or later in the week. Looking through the AlarmClock api in android I don't see a normal way to do this. Is this even possible?
Btw this is my code for setting the alarm it might not be pretty, but I am learning as I go.
package com.netwokz.setit;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
Button btnSetAlarm;
EditText etHour, etMinute;
int minute, hour, day;
Calendar cal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm);
etHour = (EditText) findViewById(R.id.etHour);
etMinute = (EditText) findViewById(R.id.etMinute);
btnSetAlarm.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_set_alarm:
setAlarm();
break;
}
}
private void setAlarm() {
cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
day = cal.get(Calendar.DAY_OF_WEEK);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_HOUR, hour + Integer.parseInt(etHour.getText().toString()));
i.putExtra(AlarmClock.EXTRA_MINUTES, minute + Integer.parseInt(etMinute.getText().toString()));
i.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
startActivity(i);
}
}
Try...this code. this code is working perfect...enjoy..full code...
MainActivity.java
public class MainActivity extends Activity {
TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;
TimePickerDialog timePickerDialog;
final static int RQS_1 = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);
buttonstartSetDialog = (Button) findViewById(R.id.startAlaram);
buttonstartSetDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textAlarmPrompt.setText("");
openTimePickerDialog(false);
}
});
}
private void openTimePickerDialog(boolean is24r) {
Calendar calendar = Calendar.getInstance();
timePickerDialog = new TimePickerDialog(MainActivity.this,
onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), is24r);
timePickerDialog.setTitle("Set Alarm Time");
timePickerDialog.show();
}
OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if (calSet.compareTo(calNow) <= 0) {
// Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet);
}
};
private void setAlarm(Calendar targetCal) {
textAlarmPrompt.setText("\n\n***\n" + "Alarm is set "
+ targetCal.getTime() + "\n" + "***\n");
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
pendingIntent);
}
}
Reciver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context k1, Intent k2) {
// TODO Auto-generated method stub
Toast.makeText(k1, "Alarm received!", Toast.LENGTH_LONG).show();
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<Button
android:id="@+id/startAlaram"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Alaram Time" />
<TextView
android:id="@+id/alarmprompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
Manifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" android:process=":remote" />
</application>
这篇关于设置机器人闹钟编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!