版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android实现闹钟小程序
这篇文章主要介绍Android实现闹钟小程序,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。最近写了个闹钟的程序,看到SharedPreferences在一个程序中可以共享数据,SharedPreferences是一个轻量级的键值存储机制,只可以存储基本数据类型。我就拿来用用,没想到SharedPreferences太好了,真是轻量级的保存数据的好的工具,比sqlite好用多了!以后我又多了一种编程思想了,呵呵,所以现在分享给大家,特别注意这点:这个无法直接在多个程序间共享Preferences数据。程序关闭再打开时间仍然保留你上次设置的时间。这就是Preferences的作用!
程序欢迎界面:点击设置闹钟界面:
点击闹钟设置中的设置后的界面:
闹钟时间到了弹出dialog:
设置重复想起闹钟后的界面:
点击返回键弹出的提示:下面请看代码:一、MainActivity中的代码:package
.daming;
import
java.util.Calendar;
import
android.app.Activity;
import
android.app.AlarmManager;
import
android.app.AlertDialog;
import
android.app.PendingIntent;
import
android.app.TimePickerDialog;
import
android.content.DialogInterface;
import
android.content.Intent;
import
android.content.SharedPreferences;
import
android.os.Bundle;
import
android.view.KeyEvent;
import
android.view.LayoutInflater;
import
android.view.MotionEvent;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.TimePicker;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity
{
TextView
setTime1;
TextView
setTime2;
TextView
setTime3;
Button
mButton1;
Button
mButton2;
Button
mButton3;
Button
mButton4;
Button
mButton5;
Button
mButton6;
String
time1String
=
null;
String
time2String
=
null;
String
time3String
=
null;
String
defalutString
=
"目前无设置";
AlertDialog
builder
=
null;
Calendar
c=Calendar.getInstance();
@Override
public
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得活动的Preferences对象
SharedPreferences
settings
=
getPreferences(Activity.MODE_PRIVATE);
time1String
=
settings.getString("TIME1",
defalutString);
time2String
=
settings.getString("TIME2",
defalutString);
time3String
=
settings.getString("TIME3",
defalutString);
InitButton1();
InitButton2();
InitButton3();
InitButton4();
InitButton5();
InitButton6();
setTime1.setText(time1String);
setTime3.setText(time2String);
setTime2.setText(time3String);
}
public
void
InitButton1()
{
setTime1=(TextView)
findViewById(R.id.setTime1);
mButton1=(Button)findViewById(R.id.mButton1);
mButton1.setOnClickListener(new
View.OnClickListener()
{
public
void
onClick(View
v)
{
c.setTimeInMillis(System.currentTimeMillis());
int
mHour=c.get(Calendar.HOUR_OF_DAY);
int
mMinute=c.get(Calendar.MINUTE);
new
TimePickerDialog(MainActivity.this,
new
TimePickerDialog.OnTimeSetListener()
{
public
void
onTimeSet(TimePicker
view,int
hourOfDay,
int
minute)
{
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY,hourOfDay);
c.set(Calendar.MINUTE,minute);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intent
intent
=
new
Intent(MainActivity.this,
CallAlarm.class);
PendingIntent
sender=PendingIntent.getBroadcast(
MainActivity.this,0,
intent,
0);
AlarmManager
am;
am
=
(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),
sender
);
String
tmpS=format(hourOfDay)+":"+format(minute);
setTime1.setText(tmpS);
//SharedPreferences保存数据,并提交
SharedPreferences
time1Share
=
getPreferences(0);
SharedPreferences.Editor
editor
=
time1Share.edit();
editor.putString("TIME1",
tmpS);
mit();
Toast.makeText(MainActivity.this,"设置大明闹钟时间为"+tmpS,
Toast.LENGTH_SHORT)
.show();
}
},mHour,mMinute,true).show();
}
});
}
public
void
InitButton2()
{
mButton2=(Button)
findViewById(R.id.mButton2);
mButton2.setOnClickListener(new
View.OnClickListener()
{
public
void
onClick(View
v)
{
Intent
intent
=
new
Intent(MainActivity.this,
CallAlarm.class);
PendingIntent
sender=PendingIntent.getBroadcast(
MainActivity.this,0,
intent,
0);
AlarmManager
am;
am
=(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
Toast.makeText(MainActivity.this,"大明闹钟时间删除",
Toast.LENGTH_SHORT).show();
setTime1.setText("目前无设置");
SharedPreferences
time1Share
=
getPreferences(0);
SharedPreferences.Editor
editor
=
time1Share.edit();
editor.putString("TIME1",
"目前无设置");
mit();
}
});
}
public
void
InitButton3()
{
setTime3=(TextView)
findViewById(R.id.setTime5);
mButton3=(Button)findViewById(R.id.mButton5);
mButton3.setOnClickListener(new
View.OnClickListener()
{
public
void
onClick(View
v)
{
c.setTimeInMillis(System.currentTimeMillis());
int
mHour=c.get(Calendar.HOUR_OF_DAY);
int
mMinute=c.get(Calendar.MINUTE);
new
TimePickerDialog(MainActivity.this,
new
TimePickerDialog.OnTimeSetListener()
{
public
void
onTimeSet(TimePicker
view,int
hourOfDay,
int
minute)
{
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY,hourOfDay);
c.set(Calendar.MINUTE,minute);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intent
intent
=
new
Intent(MainActivity.this,
CallAlarm.class);
PendingIntent
sender=PendingIntent.getBroadcast(
MainActivity.this,1,
intent,
0);
AlarmManager
am;
am
=
(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),
sender
);
String
tmpS=format(hourOfDay)+":"+format(minute);
setTime3.setText(tmpS);
//SharedPreferences保存数据,并提交
SharedPreferences
time2Share
=
getPreferences(1);
SharedPreferences.Editor
editor
=
time2Share.edit();
editor.putString("TIME2",
tmpS);
mit();
Toast.makeText(MainActivity.this,"设置大明闹钟时间为"+tmpS,
Toast.LENGTH_SHORT)
.show();
}
},mHour,mMinute,true).show();
}
});
}
public
void
InitButton4()
{
mButton4=(Button)
findViewById(R.id.mButton6);
mButton4.setOnClickListener(new
View.OnClickListener()
{
public
void
onClick(View
v)
{
Intent
intent
=
new
Intent(MainActivity.this,
CallAlarm.class);
PendingIntent
sender=PendingIntent.getBroadcast(
MainActivity.this,0,
intent,
0);
AlarmManager
am;
am
=(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
Toast.makeText(MainActivity.this,"大明闹钟时间删除",
Toast.LENGTH_SHORT).show();
setTime3.setText("目前无设置");
//SharedPreferences保存数据,并提交
SharedPreferences
time2Share
=
getPreferences(1);
SharedPreferences.Editor
editor
=
time2Share.edit();
editor.putString("TIME2",
"目前无设置");
mit();
}
});
}
public
void
InitButton5()
{
setTime2=(TextView)
findViewById(R.id.setTime2);
LayoutInflater
factory
=
LayoutInflater.from(this);
final
View
setView
=
factory.inflate(R.layout.timeset,null);
final
TimePicker
tPicker=(TimePicker)setView
.findViewById(R.id.tPicker);
tPicker.setIs24HourView(true);
final
AlertDialog
di=new
AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.clock)
.setTitle("设置")
.setView(setView)
.setPositiveButton("确定",
new
DialogInterface.OnClickListener()
{
public
void
onClick(DialogInterface
dialog,
int
which)
{
EditText
ed=(EditText)setView.findViewById(R.id.mEdit);
int
times=Integer.parseInt(ed.getText().toString())
*1000;
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY,tPicker.getCurrentHour());
c.set(Calendar.MINUTE,tPicker.getCurrentMinute());
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intent
intent
=
new
Intent(MainActivity.this,
CallAlarm.class);
PendingIntent
sender
=
PendingIntent.getBroadcast(
MainActivity.this,1,
intent,
0);
AlarmManager
am;
am
=
(AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),times,sender);
String
tmpS=format(tPicker.getCurrentHour())+":"+
format(tPicker.getCurrentMinute());
String
subStr
=
"设置大明闹钟时间为"+tmpS+
"开始,重复间隔为"+times/1000+"秒";
setTime2.setText("设置大明闹钟时间为"+tmpS+
"开始,重复间隔为"+times/1000+"秒");
//SharedPreferences保存数据,并提交
SharedPreferences
time3Share
=
getPreferences(2);
SharedPreferences.Editor
editor
=
time3Share.edit();
editor.putString("TIME3",
subStr);
mit();
Toast.makeText(MainActivity.this,"设置大明闹钟为"+tmpS+
"开始,重复间隔为"+times/1000+"秒",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消",
new
DialogInterface.OnClickListener()
{
public
void
onClick(DialogInterface
dialog,
int
which)
{
}
}).create();
mButton5=(Button)
findViewById(R.id.mButton3);
mButton5.setOnClickListener(new
View.OnClickListener()
{
public
void
onClick(View
v)
{
c.setTimeInMillis(System.currentTimeMillis());
tPicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
tPicker.setCurrentMinute(c.get(Calendar.MINUTE));
di.show();
}
});
}
public
void
InitButton6()
{
mButton6=(Button)
findViewById(R.id.mButton4);
mButton6.setOnClickListener(new
View.OnClickListener()
{
public
void
onClick(View
v)
{
Intent
intent
=
new
Intent(MainActivity.this,
CallAlarm.class);
PendingIntent
sender
=
PendingIntent.getBroadcast(
MainActivity.this,1,
intent,
0);
AlarmManager
am;
am
=
(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
Toast.makeText(MainActivity.this,"闹钟时间删除",
Toast.LENGTH_SHORT).show();
setTime2.setText("目前无设置");
//SharedPreferences保存数据,并提交
SharedPreferences
time3Share
=
getPreferences(2);
SharedPreferences.Editor
editor
=
time3Share.edit();
editor.putString("TIME3",
"目前无设置");
mit();
}
});
}
@Override
public
boolean
onKeyUp(int
keyCode,
KeyEvent
event)
{
if(keyCode
==
KeyEvent.KEYCODE_BACK){
builder
=
new
AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.clock)
.setTitle("温馨提示:")
.setMessage("您是否要退出大明闹钟程序!!!")
.setPositiveButton("确定",
new
DialogInterface.OnClickListener()
{
public
void
onClick(DialogInterface
dialog,
int
whichButton)
{
MainActivity.this.finish();
}
})
.setNegativeButton("取消",
new
DialogInterface.OnClickListener()
{
public
void
onClick(DialogInterface
dialog,
int
whichButton)
{
builder.dismiss();
}
}).show();
}
return
true;
}
private
String
format(int
x)
{
String
s=""+x;
if(s.length()==1)
s="0"+s;
return
s;
}
}二、CallAlarm中的代码:package
.daming;
import
android.content.Context;
import
android.content.Intent;
import
android.content.BroadcastReceiver;
import
android.os.Bundle;
public
class
CallAlarm
extends
BroadcastReceiver
{
@Override
public
void
onReceive(Context
context,
Intent
intent)
{
Intent
i
=
new
Intent(context,
AlarmAlert.class);
Bundle
bundleRet
=
new
Bundle();
bundleRet.putString("STR_CALLER",
"");
i.putExtras(bundleRet);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}三、AlarmAlert中的代码:package
.daming;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
public
class
AlarmAlert
extends
Activity
{
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
new
AlertDialog.Builder(AlarmAlert.this)
.setIcon(R.drawable.clock)
.setTitle("大明闹钟响了!!")
.setMessage("快完成你制定的计划吧!!!")
.setPositiveButton("关掉它",
new
DialogInterface.OnClickListener()
{
public
void
onClick(DialogInterface
dialog,
int
whichButton)
{
AlarmAlert.this.finish();
}
})
.show();
}
}四、main.xml布局文件的代码:<?xml
version="1.0"
encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="/apk/res/android"
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/other"
>
<DigitalClock
android:id="@+id/dClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="@drawable/blue"
android:layout_x="70px"
android:layout_y="32px"
>
</DigitalClock>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_title3"
android:textSize="20sp"
android:textColor="@drawable/black"
android:layout_x="10px"
android:layout_y="104px"
>
</TextView>
<Button
android:id="@+id/mButton1"
android:layout_width="wrap_content"
android:layout_height="40px"
android:text="@string/str_button1"
android:textColor="@drawable/black"
android:textSize="18sp"
android:layout_x="190px"
android:layout_y="102px"
>
</Button>
<TextView
android:id="@+id/setTime1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_default"
android:textColor="@drawable/red"
android:textSize="16sp"
android:layout_x="10px"
android:layout_y="149px"
>
</TextView>
<Button
android:id="@+id/mButton2"
android:layout_width="wrap_content"
android:layout_height="40px"
android:text="@string/str_button2"
android:textColor="@drawable/black"
android:textSize="18sp"
android:layout_x="190px"
android:layout_y="142px"
>
</Button>
<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_title4"
android:textSize="20sp"
android:textColor="@drawable/black"
android:layout_x="10px"
android:layout_y="216px"
>
</TextView>
<Button
android:id="@+id/mButton5"
android:layout_width="wrap_content"
android:layout_height="40px"
android:text="@string/str_button1"
android:textColor="@drawable/black"
android:textSize="18sp"
android:layout_x="190px"
android:layout_y="212px"
>
</Button>
<TextView
android:id="@+id/setTime5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_default"
android:textColor="@drawable/red"
android:textSize="16sp"
android:layout_x="10px"
android:layout_y="259px"
>
</TextView>
<Button
android:id="@+id/mButton6"
android:layout_width="wrap_content"
android:layout_height="40px"
android:text="@string/str_button2"
android:textColor="@drawable/black"
android:textSize="18sp"
android:layout_x="190px"
android:layout_y="252px"
>
</Button>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_title2"
android:textSize="20sp"
android:textColor="@drawable/black"
android:layout_x="10px"
android:layout_y="326px"
>
</TextView>
<Button
android:id="@+id/mButton3"
android:layout_width="wrap_content"
android:layout_height="40px"
android:text="@string/str_button1"
android:textColor="@drawable/black"
android:textSize="18sp"
android:layout_x="190px"
android:layout_y="322px"
>
</Button>
<TextView
android:id="@+id/setTime2"
android:layout_width="170px"
android:layout_height="wrap_content"
android:text="@string/str_default"
android:textColor="@drawable/red"
android:textSize="16sp"
android:layout_x="10px"
android:layout_y="369px"
>
</TextView>
<Button
android:id="@+id/mButton4"
android:layout_width="wrap_content"
android:layout_height="40px"
android:text="@string/str_button2"
android:textColor="@drawable/black"
android:textSize="18sp"
android:layout_x="190px"
android:layout_y="362px"
>
</Button>
</AbsoluteLayout>五、timeset.xml布局文件中的代码:<?xml
version="1.0"
encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/layout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="/apk/res/android"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text1"
android:textColor="@drawable/white"
android:textSize="16sp"
android:layout_x="10px"
android:layout_y="32px"
>
</TextView>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text2"
android:textColor="@drawable/white"
android:textSize="16sp"
android:layout_x="10px"
android:layout_y="172px"
>
</TextView>
<TimePicker
android:id="@+id/tPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="12px"
>
</TimePicker>
<EditText
android:id="@+id/mEdit"
android:layout_width="52px"
android:layout_height="40px"
android:text="15"
android:textSize="16sp"
android:layout_x="120px"
android:layout_y="162px"
>
</EditText>
<TextView
and
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年医院护理部年度工作总结
- 铁路工程质量检测技术规范
- 螺旋输送机自动化改造方案
- 企业资产管理与证券化操作指南
- 磨床安全操作规程
- 初一奥数题100道
- 成人英语口语提高系统训练教程
- 幼儿园家访常用谈话内容与范文
- 2026-2030清香木产业政府战略管理与区域发展战略研究咨询报告
- 2025-2030中国智能茶叶种植设备制造行业市场发展分析与发展规划报告
- 商场品牌库管理制度
- 弘法寺义工规章管理制度
- 大货车转租合同协议书
- T/ZJSEE 0016-2023高压电缆线路故障定位在线监测装置技术规范
- 2025全国英语等级考试(PETS)二级试卷:智能城市英语阅读理解
- 安全管理提升工作方案
- 小学生我的梦想课件
- 临床护理带教中的人文关怀
- 部编人教版8年级下册历史全册
- 马克思主义宗教观课件
- 项目式学习在小学数学教学中的应用
评论
0/150
提交评论