android软件开发广播闹钟(安卓闹钟语音播报)

软件开发 1302
今天给各位分享android软件开发广播闹钟的知识,其中也会对安卓闹钟语音播报进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览: 1、

今天给各位分享android软件开发广播闹钟的知识,其中也会对安卓闹钟语音播报进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用Android studio做一个可以运行的小程序,闹钟也行,然后可以连到手机,感谢大神。

环境搭建就不讲了,直接说开发。

小闹钟程序开发中的要点就是:

1、时间选择对话框(TimePicker)

2、获取闹钟管理器并对其进行设置

3、注册广播接收器

掌握了这两点,写程序就很简单了。

1、新建android项目:Alarm,sdk版本选择2.2,Package name:com.lql.activity,Main Activity:Alarm

2、编写界面:直接修改layout中的main.xml文件,代码如下:

Xml代码

?xml version="1.0" encoding="utf-8"?

LinearLayout xmlns:android=""

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center_vertical"

Button

android:id="@+id/timeBtn"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/time"

android:textSize="20sp"

/

Button

android:id="@+id/cancelAlarmBtn"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/cancelAlarm"

/

/LinearLayout

界面的效果如下:

3、修改Alarm.java这个activity,在该Activity中需要做这样几件事:

获取界面上的两个按钮组件,并给其绑定事件监听器

第一个时间按钮,点击后,显示时间选择对话框(TimePicker),供选择小时和分钟,并设置闹钟

第二个按钮,点击之后需要当前设定的闹钟

比较难写的代码就是闹钟设置:

//设置时间

Java代码  

timeBtn.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View arg0) {

Log.d(TAG, "click the time button to set time");

calendar.setTimeInMillis(System.currentTimeMillis());

new TimePickerDialog(Alarm.this,new TimePickerDialog.OnTimeSetListener() {

@Override

public void onTimeSet(TimePicker arg0, int h, int m) {

//更新按钮上的时间

timeBtn.setText(formatTime(h,m));

//设置日历的时间,主要是让日历的年月日和当前同步

calendar.setTimeInMillis(System.currentTimeMillis());

//设置日历的小时和分钟

calendar.set(Calendar.HOUR_OF_DAY, h);

calendar.set(Calendar.MINUTE, m);

//将秒和毫秒设置为0

calendar.set(Calendar.SECOND, 0);

calendar.set(Calendar.MILLISECOND, 0);

//建立Intent和PendingIntent来调用闹钟管理器

Intent intent = new Intent(Alarm.this,AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this, 0, intent, 0);

//获取闹钟管理器

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

//设置闹钟

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);

Toast.makeText(Alarm.this, "设置闹钟的时间为:"+String.valueOf(h)+":"+String.valueOf(m), Toast.LENGTH_SHORT).show();

Log.d(TAG, "set the time to "+formatTime(h,m));

}

},calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();

}

});

代码里面有注释,这里就不多解释了,其中new TimePickerDialog为创建时间选择对话框。为了能够看到效果,我给闹钟添加了重复提醒:alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);。

还要为取消闹钟按钮添加事件监听器:

Java代码  

//取消闹钟按钮事件监听

final Button cancelAlarmBtn = (Button)findViewById(R.id.cancelAlarmBtn);

cancelAlarmBtn.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View arg0) {

Intent intent = new Intent(Alarm.this,AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this, 0, intent, 0);

//获取闹钟管理器

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

alarmManager.cancel(pendingIntent);

Toast.makeText(Alarm.this, "闹钟已经取消!", Toast.LENGTH_SHORT).show();

}

});

在点击取消闹钟按钮时,取消之前设置的闹钟,核心代码就4行。

4、编写广播接收器,用来接收闹钟的广播事件,然后进行相关处理,

Java代码  

public class AlarmReceiver extends BroadcastReceiver {

/* (non-Javadoc)

* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)

*/

@Override

public void onReceive(Context arg0, Intent data) {

Log.d(Alarm.TAG, "the time is up,start the alarm...");

Toast.makeText(arg0, "闹钟时间到了!", Toast.LENGTH_SHORT).show();

}

}

这个代码就很简单了,主要是要继 承 BroadcastReceiver 这个类,然后重写onRecive方法。onRecive方法在闹钟的时间达到之后会执行,在这里我们可以做自己的事情,比如启动某个程序,或者播放铃声,我这里就是简单的提示一下,使用的是Toast。

5、在android的AndroidManifest.xml文件中注册广播接收器:

manifest xmlns:android=""

Xml代码

package="com.ql.activity"

android:versionCode="1"

android:versionName="1.0"

application android:icon="@drawable/icon" android:label="@string/app_name"

receiver android:name=".AlarmReceiver" android:process=":remote" /

activity android:name=".Alarm"

android:label="@string/app_name"

intent-filter

action android:name="android.intent.action.MAIN" /

category android:name="android.intent.category.LAUNCHER" /

/intent-filter

/activity

/application

uses-sdk android:minSdkVersion="8" /

/manifest

核心的配置为receiver android:name=".AlarmReceiver" android:process=":remote" /,这也是闹钟程序的关键,如果不做这个配置,那么时间到了之后,闹钟将不会提示。

接下来就是到模拟器上测试,运行截图如上图。程序源代码见附件。

Android开发一个简单实用的闹铃APP

生活中我们会常常遇到需要闹钟提醒;不管是起床还是生活中的事件提醒。

那作为Android开发如何自己开发一个闹钟功能呢,是不是觉得很酷呢?接下来我们就实战一个闹钟。

本示例采用的是RecyclerView,其适配器类与常无二,其异在于继承一个代理类,为适配之后侧滑删除而准备

建立一个内部类ViewHolder实现控件定义申明

实现onCreateViewHolder方法载入子项布局文件

绑定实体类,实现onBindViewHolder获取数据

此处有三处状态,第一种状态:第一次进入程序,默认加载固定闹钟子项;第二种状态:进入添加子项页面,然后返回其主页面,并判断其switch是否为ture,如果为ture则添加子项;第三种状态:程序被系统回收或者用户停止程序,并再次进入程序,防止加载前一时刻闹钟子项;

添加依赖 实现侧滑主要依赖于一个第三方包,然后使用RecyclerView进行子项绑定 依赖如下:

并在目录build.gradle包下添加如下库

其实现侧滑删除主要的玄机在于布局文件当中,使用RelativeLayout布局,将删除按钮固定在布局右方,并使用其他布局将其覆盖,只有滑动时,才将其显示。掩盖侧滑删除按钮与暴露侧滑删除按钮效果对比图如下

然后,在适配器类中,实现WeSwipeHelper.SwipeLayoutTypeCallBack接口,实现如下三个方法,第一个方法为获取侧滑删除按钮的宽度;第二个方法为需要滑动的视图,也就是覆盖侧滑删除按钮的布局;第三个方法为当视图正在滑动时,用户触发单击事件,自动还原滑动状态

最后,在需要添加子项的视图中绑定RecyclerView即可

通过监听子项滑动删除按钮点击事件,实现子项删除

跳转新增闹钟子项Acticity需要传输实体类对象,传输对象一般需要序列化改类,其操作如下

定义实体类,并实现序列化

然后通过Intent传输Bundle对象

实现时间选择主要使用系统集成的组件TimePicker,其使用方法如下 其有两种显示方式,第一种为spinner,就是下拉滑动式,第二种为clock,即显示一个时钟,通过滑动指针选择时间

在style.xml文件中申明如下样式

然后再指定Activcty申明即可

获取数据比较简单,实现对应接口即可

将获取的数据通过SharedPreferences存储起来,然后点击存储时,进行页面跳转,然后再该界面进行取出数据

存储数据

首先判断回调的switch数据是否为ture,如果为ture则保存该子项,然后再适配器类中进行数据添加

选中与默认两种状态效果图如下

创建thumb和track样式

创建一个选择器文件,有选中和默认两种状态

创新open_thumb.xml文件

创建shut_thumb.xml文件

同样创建一个选择器,并用于两种状态

其中AlarmManager为系统主要操作类,参数为提醒模式、提醒时间(long型)、PendingIntent对象 以下有三种时间传入,第一种,直接传入一个Long型时间用于测试,第二种,通过设置系统启动至今而设置时间,第三种,通过取出设置的时间,然后获取系统当前时间,将其差传入其中。

然后再清单文件中注册服务

使用Intent实现服务启动

杀死程序

本示例总共使用到了三个单例类:SP(SharedPreferences封装)、TimeFormat(时间数据格式封装)、KillProcess(杀死所有Activity)

SharedPreferences

KillProcess

文章带这里就完成了一个简单的闹钟;Android开发还有许多更加更多的知识学习。进一步学习Android技术,我这里推荐这份笔记方便学习,我就放在私信, 发送“核心笔记”或“手册”即可获取。朋友们可以免费领取!

Android开发——定时器处理

  开发中经常遇到用计时器的时候,例如:每秒输出一次log,这时候就要用到延时操作,这里总结了几种常用的方法。

   AlarmManager 是Android的全局定时器,可以在指定时间执行定时任务。定时任务通过PendingIntent 的 getActivity()、getService()或 getBroadcast() 来执行。简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为我们广播一个我们设定的Intent出来,在接收到设定的Intent时执行定时任务。

(2) setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法用于设置重复闹钟,其中参数说明如下:

type:闹钟类型

startTime:首次执行闹钟的时间

intervalTime:两次执行闹钟的时间间隔

pi:闹钟响应动作

(3) setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法也用于设置重复闹钟,与第二个方法相似,区别在于其两次闹钟执行的间隔时间不是固定的。

   AlarmManager实现定时的方法,我还没有用到过,以上内容是在网上查找资料后自己在一个小demo中小小试验了的,有错误或描述不清的地方请见谅。下面是我参考的介绍AlarmManager的三篇文章。

android中如何实现定时提醒

android中可以使用闹钟进行提醒,你只需要告知系统你想在什么时候被提醒,然后需要一个闹钟的广播接收器,当到你设置的时间时,系统会给你发送一条广播,当你接收到广播后你就可以做一些操作,比如启动你的app,或者跳转到你app中的任何一个界面。代码如下:

//发送闹钟请求

Intent intent = new Intent(mContext, AlarmReceiver.class);

intent.setAction("something");

intent.setType("something");

intent.setData(Uri.EMPTY);

intent.addCategory(“something”);

intent.setClass(context, AlarmReceiver.class);

// 以上给intent设置的四个属性是用来区分你发给系统的闹钟请求的,当你想取消掉之前发的闹钟请求,这四个属性,必须严格相等,所以你需要一些比较独特的属性,比如服务器返回给你的json中某些特定字段。

//当然intent中也可以放一些你要传递的消息。

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarmCount, intent, 0);

//alarmCount是你需要记录的闹钟数量,必须保证你所发的alarmCount不能相同,最后一个参数填0就可以。

AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

//这样闹钟的请求就发送出去了。time是你要被提醒的时间,单位毫秒,注意不是时间差。第一个参数提醒的需求用我给出的就可以,感兴趣的朋友,可以去google一下,这方面的资料非常多,一共有种,看一下就知道区别了。

//取消闹钟请求

Intent intent = new Intent(mContext, AlarmReceiver.class);

intent.setAction("something");

intent.setType(something);

intent.setData(Uri.EMPTY);

intent.addCategory(something);

intent.setClass(context, AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alarmCount, intent, 0);

//alarmCount对应到你设定时的alarmCount,

AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

am.cancel(pendingIntent);

//接着,你需要一个广播接收的类:

public class AlarmReceiver extends BroadcastReceiver{

private NotificationManager manager;

@Override

public void onReceive(Context context, Intent intent) {

manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

//例如这个id就是你传过来的

String id = intent.getStringExtra("id");

//MainActivity是你点击通知时想要跳转的Activity

Intent playIntent = new Intent(context, MainActivity.class);

playIntent.putExtra("id", id);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setContentTitle("title").setContentText("提醒内容").setSmallIcon(R.drawable.app_icon).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true).setSubText("二级text");

manager.notify(1, builder.build());

}

}

android AlarmManager使用详解

AlarmManager是android中系统自带的一个提醒服务,比如设置闹钟,做一个定时任务,还可以设置重复操作

AlarmManager中常用的有三个方法:

1、set(int type,long startTime,PendingIntent pi),用于设置一次闹钟。

2、setRepeating(int type,long startTime,long intervalTime,PendingIntent pi),用于设置重复闹钟。

3、setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi),同样是用于设置重复闹钟,但是它是不准确的,相对于第二个方法,也更加节能。

下面就看看这些方法中的参数:

type为闹钟的类型,可分为四个常量:

ELAPSED_REALTIME:闹钟在睡眠状态下不可用,使用的是相对系统启动时间。

ELAPSED_REALTIME_WAKEUP:闹钟在睡眠状态下可用,使用的是相对系统启动时间。

RTC:闹钟在睡眠状态下不可用,使用的是真实时间。

RTC_WAKEUP:闹钟在睡眠状态下可用,使用的是真实时间。

startTime:为开始时间

intervalTime:为重复闹钟的间隔时间,内置了几种:

INTERVAL_FIFTEEN_MINUTES 15分钟

INTERVAL_HALF_HOUR 半个小时

INTERVAL_HOUR 一个小时

INTERVAL_HALF_DAY 半天

INTERVAL_DAY 一天

PendingIntent :广播的一个intent,我们用广播接受闹钟的定时任务,注册一个广播去接受,任务指令。

使用PendingIntent的getBroadcast (Context context, int requestCode, Intent intent, int flags)方法可以得到一个发送广播动作的PendingIntent对象

为以下4个常量或其他支持使用Intent.fillIn()来控制它的变量:

FLAG_CANCEL_CURRENT:如果描述的PendingIntent对象已经存在时,会先取消当前的PendingIntent对象再生成新的。

FLAG_NO_CREATE:如果描述的PendingIntent对象不存在,它会返回null而不是去创建它。

FLAG_ONE_SHOT:创建的PendingIntent对象只使用一次。

FLAG_UPDATE_CURRENT:如果描述的PendingIntent对象存在,则保留它,并将新的PendingIntent对象的数据替换进去。

另一个就是在manifest.xml中注册自己的广播

这样就可以实现定时任务了,如果要定时任务service启动还可以加上service

android软件开发广播闹钟的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于安卓闹钟语音播报、android软件开发广播闹钟的信息别忘了在本站进行查找喔。

扫码二维码