用alarmManager来设置自己的闹钟,其中我们主要用到的类有
创新互联公司是专业的滨海新区网站建设公司,滨海新区接单;提供成都网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行滨海新区网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
- import Android.app.AlarmManager;
- import android.app.PendingIntent;
- import android.app.TimePickerDialog;
这三个是我们必须要用到的类,大家可要记住呀,可不能用错包呀,还有就是最重要的一点,也是大家容易忘了的一点,我们一定要加上权限,这样我们才能顺利的实现我们想要的效果,代码如下:
java代码:
- package eoe.demo;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.widget.Toast;
- public class AlamrReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- Toast.makeText(context, "闹钟时间到", Toast.LENGTH_LONG).show();
- }
- }
Java代码:
- package eoe.demo;
- import java.util.Calendar;
- import android.app.Activity;
- import android.app.AlarmManager;
- import android.app.PendingIntent;
- import android.app.TimePickerDialog;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.TimePicker;
- public class Test extends Activity {
- /** Called when the activity is first created. */
- private TextView tv = null;
- private Button btn_set = null;
- private Button btn_cel = null;
- private Calendar c = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv = (TextView) this.findViewById(R.id.TextView);
- btn_set = (Button) this.findViewById(R.id.Button01);
- btn_cel = (Button) this.findViewById(R.id.Button02);
- c = Calendar.getInstance();
- btn_set.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v) {
- // TODO Auto-generated method stub
- c.setTimeInMillis(System.currentTimeMillis());
- int hour = c.get(Calendar.HOUR_OF_DAY);
- int minute = c.get(Calendar.MINUTE);
- new TimePickerDialog(Test.this,new TimePickerDialog.OnTimeSetListener(){
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- // TODO Auto-generated method stub
- 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(Test.this,AlamrReceiver.class);
- PendingIntent pi = PendingIntent.getBroadcast(Test.this, 0, intent, 0);
- AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
- am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);//设置闹钟
- am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), (10*1000), pi);//重复设置
- tv.setText("设置的闹钟时间为:"+hourOfDay+":"+minute);
- }
- },hour,minute,true).show();
- }
- });
- btn_cel.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(Test.this,AlamrReceiver.class);
- PendingIntent pi = PendingIntent.getBroadcast(Test.this, 0, intent, 0);
- AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
- am.cancel(pi);
- tv.setText("闹钟取消");
- }
- });
- }
- }
Java代码:
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- android:id="@+id/TextView"
- android:layout_height="wrap_content" android:text="@string/hello" />
Java代码:
- package="eoe.demo"
- android:versionCode="1"
- android:versionName="1.0">
- android:label="@string/app_name">
【编辑推荐】
Android开发中插入新的Activity
Android 多任务多线程断点下载
Android开发之旅:Android架构
常用的Android指令和模拟器参数
在Android应用程序中使用Internet数据
网页标题:Android中Alarm的使用
URL地址:http://www.shufengxianlan.com/qtweb/news43/39693.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联