Android闹钟动画,Android实现闹钟小程序
最近写了个闹钟的程序,看到SharedPreferences在一个程序中可以共享数据,SharedPreferences是一个轻量级的键值存储机制,只可以存储基本数据类型。我就拿来用用,没想到SharedPreferences太好了,真是轻量级的保存数据的好的工具,比sqlite好用多了!以后我又多了一种编程思想了,呵呵,所以现在分享给大家,特别注意这点:这个无法直接在多个程序间共享Preferences数据。程序关闭再打开时间仍然保留你上次设置的时间。这就是Preferences的作用!
程序欢迎界面:
点击设置闹钟界面:
点击闹钟设置中的设置后的界面:
闹钟时间到了弹出dialog:
设置重复想起闹钟后的界面:
点击返回键弹出的提示:
下面请看代码:
一、MainActivity中的代码:
package com.cn.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);
editor.commit();
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", "目前无设置");
editor.commit();
}
});
}
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);
editor.commit();
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", "目前无设置");
editor.commit();
}
});
}
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);
editor.commit();
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", "目前无设置");
editor.commit();
}
});
}
@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 com.cn.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 com.cn.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布局文件的代码:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/other"
>
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"
>
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"
>
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"
>
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"
>
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"
>
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"
>
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"
>
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"
>
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"
>
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"
>
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"
>
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"
>
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"
>
五、timeset.xml布局文件中的代码:
android:id="@+id/layout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
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"
>
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"
>
android:id="@+id/tPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="12px"
>
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"
>
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text3"
android:textColor="@drawable/white"
android:textSize="16sp"
android:layout_x="180px"
android:layout_y="172px"
>
六、string.xml中的代码:
Hello World, EX06_10
大明原创闹钟
设置闹钟
删除闹钟
重复响起的闹钟
大明闹钟一
大明闹钟二
目前无设置
开始时间
重复响起的闹钟
秒
七、color.xml中的代码:
#FFFFFFFF
#FF00FFF0
#000000
#0000FF
#FF0000
八、AndroidManifest.xml中的代码
package="com.cn.daming"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
android:label="@string/app_name">
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
Android闹钟动画,Android实现闹钟小程序相关推荐
- Android studio开发-单界面单机小程序
Android studio开发-单界面单机小程序 最终结果展示 步骤: 建立项目 file-new-new project 新建一个项目文件 选择一个empty activity 配置项目名称以及项 ...
- Android安卓的家教平台设计小程序app毕业设计
Android安卓的家教平台设计小程序app毕业设计
- android 齿轮动画,Android仿正点闹钟时间齿轮滑动效果
看到正点闹钟上的设置时间的滑动效果非常好看,自己就想做一个那样的,在网上就开始搜资料了,看到网上有的齿轮效果的代码非常多,也非常难懂,我就决定自己研究一下,现在我就把我的研究成果分享给大家.我研究的这 ...
- android img标签属性_微信小程序 组件叠加效果(如 Android 中的添加蒙层)
实现的效果如下: 可以看出这是由image组件和text组件叠加到一块组成的蒙层效果. 在小程序中实现这个效果主要用到z-index属性和position属性 z-index的使用必须是双方组件都设置 ...
- 微信 android兼容性问题怎么解决方案,微信小程序兼容性问题
本文我们来谈谈微信小程序系统兼容性的那些坑. 微信小程序兼容性问题 微信小程序发布一周多了,兼容性问题,特别是 Android 平台兼容性问题特别严重.据我观察,好多小程序掉到兼容性的坑里.掉坑里不要 ...
- android开发面试题!微信小程序趋势及前景,社招面试心得
没有稳定的工作,只有稳定的能力. 又到了万物复苏的季节,在程序猿这个行当里,作为 Android 开发出生的,在经历了八年的脱发生涯后,有了越来越多的想法和感触 趋势 随着各类移动跨平台的兴起,在 R ...
- android分享朋友圈功能,微信小程序实现分享至朋友圈的功能来啦
微信小程序「分享至朋友圈」能力,终于来了!(之前,我相信大部分微信小程序的开发者都是用"分享卡片"的形式,手动调用wx.createCanvasContext生成一张图片,让用户生 ...
- android微信自动化脚本,appium——微信小程序自动化
由于腾讯系QQ.微信是基于腾讯自研X5内核-类似webview,不是谷歌原生webview,所以调试会有些许差异(有很 多app厂商也开始采用X5内核) 微信小程序自动化测试只能够支持手机,模拟器是不 ...
- android 上下左右滑动的表格,适用小程序的表格table,冻结行,冻结列,可左右,上下滑动...
更新记录 1.0.0(2020-07-22) 1.暂时适用于微信小程序, 2.点击首列跳转或提示 3.首列合并行,首列背景色不一样 4.点击行变背景色 平台兼容性 app 微信小程序 支付宝小程序 百 ...
最新文章
- C++练习 | C++中Vector的使用
- docker入门与实践之【04-使用dockerfile定制镜像】
- 使用supervisor启动hbase
- 20155212 2017-2018-1 《信息安全系统设计》第8周课下作业
- Hadoop学习资料
- hibernate3配置文件hibernate.cfg.xml的详细解释
- 关于JavaBean
- 基于主体掩码的实体关系抽取方法
- STM32H743+CubeMX-定时器TIM发送非对称PWM(使用一个通道)
- linux:tomcat写入文件失败
- 基于顺序存储结构的图书信息表的图书去重(C++)
- 1005 继续(3n+1)猜想(25 分)
- C++的基础知识有哪些?
- 转载---ubutun18.04系统安装搜狗输入法
- 安装blocksci mac出错_你的Mac与Big Sur兼容吗?
- 黑莓7100T激活上网、彩信设置(转)
- 【大数据搜索引擎案例汇总】
- mysql 聚簇索引和非聚簇索引
- 什么是SOA?为什么要SOA?
- mysql服务启动、停止、重启
热门文章
- 大数据IMF传奇行动绝密课程第45课:Spark性能优化第一季
- galaxy s8 android8.0,【三星资讯】三星终于正式升级安卓8.0!Galaxy S8尝鲜
- 解读艾瑞2009年一季度网络行业报告
- fatal error C1189: #error 错误解决方案
- html一键清空数组中的内容,前端页面中JS和Jquery框架对数组遍历,添加,删除,清空的一些使用...
- 高通平台开发系列讲解(Camera篇)新增GC8034摄像头步骤
- BitDock主题软件推荐
- 小小输入法的郑码练习
- js 数组转字符串,字符串转数组
- 计算机二级Python真题(四)