博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android菜鸟的成长笔记(25)——可爱的小闹钟
阅读量:6111 次
发布时间:2019-06-21

本文共 14449 字,大约阅读时间需要 48 分钟。

摘要:

这一篇主要使用系统为我们提供的一个服务AlarmManager来制作一个Android小闹钟,同时还涉及到了自定义主题、判断第一次启动应用、自定义动画、对话框、制作指导滑动页面等方面。最后形成一个可以直接在手机上面使用的小闹钟。

开始启动界面AppStart.java

package com.example.alarmtest;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.widget.LinearLayout;public class AppStart extends Activity{	public static final String PACKAGE_NAME = "com.example.alarmtest";	public static final String VERSION_KEY = "versionCode";	SharedPreferences preferences;	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);				//判断是否是首次安装		/** 判断应用首次运行 **/				preferences = getSharedPreferences("count",MODE_WORLD_READABLE);        int count = preferences.getInt("start_count", 0);        if(count == 0){        	Editor editor = preferences.edit();            //存入数据            editor.putInt("start_count", ++count);            //提交修改	            editor.commit();            			Intent intent = new Intent(AppStart.this, GuideActivity.class);			startActivity(intent);			AppStart.this.finish();					}else{			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(					LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);			LinearLayout linearLayout = new LinearLayout(this);			linearLayout.setLayoutParams(params);			linearLayout.setOrientation(LinearLayout.VERTICAL);			linearLayout.setBackgroundResource(R.drawable.main_bg_default_img_2);						new Handler().postDelayed(new Runnable(){				@Override				public void run(){					Intent intent = new Intent (AppStart.this, MainActivity.class);								startActivity(intent);								AppStart.this.finish();				}			}, 1000);			setContentView(linearLayout);		}			}}
上面使用sharedPreference中的默认设值来判断该应用是否是第一次启动,如果是第一次启动则启动GuidActivity进入指导页面,如果不是第一次启动则启动MainActivity进入主界面。

先看看GuidActivity的实现

package com.example.alarmtest;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.Parcelable;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.view.Window;import android.view.WindowManager;import android.widget.ImageView;/** * 第一次安装引导用户的Activity * @author lixq * */public class GuideActivity extends Activity{	     private ViewPager viewPager;         private ArrayList
pageViews; private ImageView imageView; private ImageView[] imageViews; // 包裹滑动图片LinearLayout private ViewGroup main; // 包裹小圆点的LinearLayout private ViewGroup group; //左箭头按钮 private ImageView imageViewLeft; //右箭头按钮 private ImageView imageViewRight; //当前页码 private int currentIndex; //ImageView的alpha值 private int mAlpha = 0; private boolean isHide; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //将要显示的图片放到ArrayList当中,存到适配器中 LayoutInflater inflater = getLayoutInflater(); pageViews = new ArrayList
(); pageViews.add(inflater.inflate(R.layout.guide_item1, null)); pageViews.add(inflater.inflate(R.layout.guide_item2, null)); pageViews.add(inflater.inflate(R.layout.guide_item3, null)); pageViews.add(inflater.inflate(R.layout.guide_item4, null)); //将图片存放到ImageView集合中 imageViews = new ImageView[pageViews.size()]; main = (ViewGroup)inflater.inflate(R.layout.guide, null); //获取存放底部导航点ViewGroup group = (ViewGroup)main.findViewById(R.id.guide_point_ll); viewPager = (ViewPager)main.findViewById(R.id.guide_viewpager); imageViewLeft = (ImageView)main.findViewById(R.id.imageView1); imageViewRight = (ImageView)main.findViewById(R.id.imageView2); imageViewLeft.setAlpha(0); imageViewRight.setAlpha(0); //将小圆点放到imageView数组当中 for (int i = 0; i < pageViews.size(); i++) { System.out.println("pageViews.size() = " + pageViews.size()); imageView = new ImageView(GuideActivity.this); imageView.setLayoutParams(new LayoutParams(20,20)); imageView.setPadding(20, 0, 20, 0); imageViews[i] = imageView; if (i == 0) { //默认选中第一张图片 imageViews[i].setBackgroundResource(R.drawable.green_point); } else { imageViews[i].setBackgroundResource(R.drawable.gray_point); } group.addView(imageViews[i]); } setContentView(main); viewPager.setAdapter(new GuidePageAdapter()); viewPager.setOnPageChangeListener(new GuidePageChangeListener()); imageViewLeft.setOnClickListener(new ButtonListener()); imageViewRight.setOnClickListener(new ButtonListener()); } //左右切换屏幕的按钮监听器 class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub int showNext=0; if(v.getId() == R.id.imageView1) { System.out.println("点击了向左的按钮"); if(currentIndex ==0 ) showNext = currentIndex; else showNext = currentIndex-1; viewPager.setCurrentItem(showNext); } if(v.getId() == R.id.imageView2){ System.out.println("点击了向右的按钮"); if(currentIndex == imageViews.length) showNext = currentIndex; else showNext = currentIndex+1; viewPager.setCurrentItem(showNext); } System.out.println("当前页码:"+showNext); } } /** * 设置按钮渐显效果 */ private Handler mHandler = new Handler() { public void handleMessage(Message msg) { if(msg.what==1 && mAlpha<255){ //通过设置不透明度设置按钮的渐显效果 mAlpha += 50; if(mAlpha>255) mAlpha=255; imageViewLeft.setAlpha(mAlpha); imageViewLeft.invalidate(); imageViewRight.setAlpha(mAlpha); imageViewRight.invalidate(); if(!isHide && mAlpha<255) mHandler.sendEmptyMessageDelayed(1, 100); }else if(msg.what==0 && mAlpha>0){ mAlpha -= 3; if(mAlpha<0) mAlpha=0; imageViewLeft.setAlpha(mAlpha); imageViewLeft.invalidate(); imageViewRight.setAlpha(mAlpha); imageViewRight.invalidate(); if(isHide && mAlpha>0) mHandler.sendEmptyMessageDelayed(0, 2); } } }; private void showImageButtonView(){ isHide = false; mHandler.sendEmptyMessage(1); } private void hideImageButtonView(){ new Thread(){ public void run() { try { isHide = true; mHandler.sendEmptyMessage(0); } catch (Exception e) { ; } } }.start(); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { System.out.println("this is dispatch"); System.out.println("触碰屏幕"); switch (ev.getAction()) { case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_DOWN: showImageButtonView(); break; case MotionEvent.ACTION_UP: hideImageButtonView(); break; } return super.dispatchTouchEvent(ev); } // 指引页面数据适配器,实现适配器方法 class GuidePageAdapter extends PagerAdapter { @Override public int getCount() { return pageViews.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return super.getItemPosition(object); } @Override public void destroyItem(View arg0, int arg1, Object arg2) { // TODO Auto-generated method stub ((ViewPager) arg0).removeView(pageViews.get(arg1)); } @Override public Object instantiateItem(View arg0, int arg1) { // TODO Auto-generated method stub ((ViewPager) arg0).addView(pageViews.get(arg1)); return pageViews.get(arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { // TODO Auto-generated method stub } @Override public Parcelable saveState() { // TODO Auto-generated method stub return null; } @Override public void startUpdate(View arg0) { // TODO Auto-generated method stub } @Override public void finishUpdate(View arg0) { // TODO Auto-generated method stub } } // 指引页面更改事件监听器,左右滑动图片时候,小圆点变换显示当前图片位置 class GuidePageChangeListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { currentIndex = arg0; for (int i = 0; i < imageViews.length; i++) { imageViews[arg0].setBackgroundResource(R.drawable.green_point); if (arg0 != i) { imageViews[i].setBackgroundResource(R.drawable.gray_point); } } } }}
使用ViewPage来显示各个布局文件,在四个布局文件中分别放四张图片。

详情请看:http://blog.csdn.net/dawanganban/article/details/17305769

进入主界面后就是我们设置闹钟的界面了,实现如下:

package com.example.alarmtest;import java.util.Calendar;import android.app.Activity;import android.app.AlarmManager;import android.app.PendingIntent;import android.app.Service;import android.app.TimePickerDialog;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TimePicker;import android.widget.Toast;public class MainActivity extends Activity {	Button button;	AlarmManager alarmManager;	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		button = (Button) findViewById(R.id.button1);				//获取AlarmManager对象		alarmManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);				button.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View arg0) {				Calendar currentTime = Calendar.getInstance();				//创建一个TimePickerDialog实例,并显示				new TimePickerDialog(MainActivity.this, 0, 						new TimePickerDialog.OnTimeSetListener() {														@Override							public void onTimeSet(TimePicker view, int hourOfDay, int minute) {								//指定启动AlarmActivity组件								Intent intent = new Intent(MainActivity.this, AlarmActivity.class);								//创建PendingIntent对象								/*								 * PendingIntent与Intent的区别是PendingIntent处理即将发生的事情								 * 比如:在通知栏Notification中跳转页面,不是立即跳转								 * 通常通过  getActivity、getBroadcast、getService得到PendingIntent的实例								 * 								 */								PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);								Calendar c = Calendar.getInstance();								c.setTimeInMillis(System.currentTimeMillis());								c.set(Calendar.HOUR, hourOfDay);								c.set(Calendar.MINUTE, minute);																alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);								Toast.makeText(MainActivity.this, "设置闹钟成功", Toast.LENGTH_SHORT).show();							}				}, currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), false).show();			}		});	}}
上面使用了一个时间设置组件TimePickDialog来设置时间,设置完时间后使用AlarmManager的set方法设置闹钟,值得注意的是上面有一个PendingIntent,这个和Intent的区别是PendIntent表示即将发生的意图,常和AlarmManager与Notifycation同时使用。另外,AlarmManager.RTC_WAKEUP表示一个定时器且会发出警报。

闹钟到时间后启动AlarmActivity来播放音乐

package com.example.alarmtest;import android.app.Activity;import android.app.AlertDialog;import android.app.Notification;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.media.MediaPlayer;import android.os.Bundle;public class AlarmActivity extends Activity{	MediaPlayer alarmMusic;	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		//加载指定音乐,并为之创建MediaPlayer对象		alarmMusic = MediaPlayer.create(this, R.raw.nswdy);		alarmMusic.setLooping(true);		//播放闹钟		alarmMusic.start();		//创建一个对话框		new AlertDialog.Builder(AlarmActivity.this).setTitle("闹钟")			.setMessage("闹钟响了,快起床啦!")			.setPositiveButton("确定", new OnClickListener() {								@Override				public void onClick(DialogInterface dialog, int which) {					//停止音乐					alarmMusic.stop();					AlarmActivity.this.finish();				}			}).show();	}}

最后的运行结果:

                   

                  

源代码下载:http://download.csdn.net/detail/lxq_xsyu/6963763

转载于:https://www.cnblogs.com/lanzhi/p/6469177.html

你可能感兴趣的文章
java的特殊符号
查看>>
word2010中去掉红色波浪线的方法
查看>>
fabric上下文管理器(context mangers)
查看>>
JQuery-EasyUI Datagrid数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)
查看>>
并发和并行的区别
查看>>
SWATCH
查看>>
JSP中行的查询
查看>>
Adhesive框架系列文章--内存队列服务模块使用和实现
查看>>
Ruby学习笔记-循环与选择结构
查看>>
整数的转换成2进制有多少个1
查看>>
在SQL Server 的使用过程中,发现几个很有用,但不太常用
查看>>
[转]用Mochiweb打造百万级Comet应用,第三部分
查看>>
初级程序员的学习方法见解
查看>>
swap配置小结
查看>>
[原创]FineUI秘密花园(八) — 下拉列表控件
查看>>
让你的Rt.jar能够包含元信息
查看>>
单元测试(H2等)和持续集成(Hudson)实战简介
查看>>
CSS小技巧二:导航中鼠标经过变换文字
查看>>
Android 读取文件内容
查看>>
C#方法参数传递-引用参数ref关键字
查看>>