大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android中怎么实现一个闹钟小程序-创新互联

这期内容当中小编将会给大家带来有关Android中怎么实现一个闹钟小程序,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

目前创新互联已为近千家的企业提供了网站建设、域名、虚拟空间、网站托管、企业网站设计、孝义网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

一、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布局文件的代码:

               

五、timeset.xml布局文件中的代码:

六、string.xml中的代码:

Hello World, EX06_10 大明原创闹钟 设置闹钟 删除闹钟 重复响起的闹钟 大明闹钟一 大明闹钟二 目前无设置 开始时间 重复响起的闹钟

七、color.xml中的代码:

#FFFFFFFF #FF00FFF0 #000000 #0000FF #FF0000

八、AndroidManifest.xml中的代码

                         

上述就是小编为大家分享的Android中怎么实现一个闹钟小程序了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


分享标题:Android中怎么实现一个闹钟小程序-创新互联
当前URL:http://dzwzjz.com/article/ddchio.html
在线咨询
服务热线
服务热线:028-86922220
TOP