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

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
 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"
>
 <DigitalClock
  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"
 >
 </DigitalClock>
  <TextView
   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"
  >
  </TextView>
  <Button
   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"
  >
   </Button>

   <TextView
   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"
  >
  </TextView>
  <Button
   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"
  >
  </Button>

  <TextView
   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"
  >
  </TextView>
  <Button
   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"
  >
   </Button>

   <TextView
   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"
  >
  </TextView>
  <Button
   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"
  >
  </Button>

  <TextView
   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"
  >
  </TextView>
  <Button
   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"
  >
  </Button>

  <TextView
   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"
  >
   </TextView>
  <Button
   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"
  >
  </Button>

</AbsoluteLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
 android:id="@+id/layout2"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android"
>
 <TextView
  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"
 >
 </TextView>
 <TextView
  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"
 >
 </TextView>
 <TimePicker
  android:id="@+id/tPicker"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_x="100px"
  android:layout_y="12px"
 >
 </TimePicker>
 <EditText
  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"
 >
 </EditText>
 <TextView
  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"
 >
 </TextView>
</AbsoluteLayout>

六、string.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">Hello World, EX06_10</string>
 <string name="app_name">大明原创闹钟</string>
 <string name="str_button1">设置闹钟</string>
 <string name="str_button2">删除闹钟</string>
 <string name="str_title2">重复响起的闹钟</string>
 <string name="str_title3">大明闹钟一</string>
 <string name="str_title4">大明闹钟二</string>
 <string name="str_default">目前无设置</string>
 <string name="str_text1">开始时间</string>
 <string name="str_text2">重复响起的闹钟</string>
 <string name="str_text3">秒</string>
</resources>

七、color.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <drawable name="white">#FFFFFFFF</drawable>
 <drawable name="other">#FF00FFF0</drawable>
 <drawable name="black">#000000</drawable>
 <drawable name="blue">#0000FF</drawable>
 <drawable name="red">#FF0000</drawable>
</resources>

 八、AndroidManifest.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.cn.daming"
   android:versionCode="1"
   android:versionName="1.0">
  <uses-sdk android:minSdkVersion="8" />

  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <!-- 註冊receiver CallAlarm -->
    <receiver android:name=".CallAlarm" android:process=":remote" />
    <activity android:name=".MainActivity"
         android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".AlarmAlert"
       android:label="@string/app_name">
    </activity>
  </application>
</manifest> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 简单实现Android闹钟程序 附源码

    这个应用实现了简单的闹钟功能,关闭程序的状态中也可以进行闹钟的提醒. 这里遗留了一个问题: 如果我通过应用本身关闭程序,是可以实现我上述所说的功能,但是如果我在手机进程管理中实现应用程序的关闭操作,这个提醒就不起作用了,我很疑惑的是,我通过应用程序本身关闭了后,在进程中也是查看不到这个应用了的.所以哪位知道的帮留个言指点下,谢谢. ClockDemo.java package com.example.clock; import java.util.Calendar; import android

  • Android闹钟启动时间设置无效问题的解决方法

    Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题 做一个app,需要后台保持发送心跳包.由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmManager定时唤醒Service发送心跳包. 以下是开启alarmManager的代码 //开启轮询服务 public static void startPollingService(Context context, int seconds, Class<?> cls,String action) {

  • Android手机闹钟用法实例

    本文实例讲述了Android手机闹钟用法.分享给大家供大家参考.具体如下: 一.开发手机闹钟主要用到了AlarmManager类,AlarmManager类提供了访问系统定时服务的途径,开发人员可以在程序中设置某个应用程序在未来的某个时刻被执行.当AlarmManager定时时间到了之后,当初注册的Intent对象将会被系统广播,进而启动目标程序. 二.在程序运行时当需要使用AlarmManager类时,可以通过Context对象的getSystemService(Context.ALARM_S

  • Android通过AlarmManager类实现简单闹钟功能

    Android通过AlarmManager类实现闹钟,供大家参考,具体内容如下 简介 闹钟是生活中最常用的功能了,很多App都可以加入该功能,提醒用户某个时刻要做的事情.在Android系统中可以通过AlarmManager类实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件.AlarmManager提供了访问系统警报的服务,只要在程序中设置了警报服务,AlarmManager就会通过onReceive()方法去还行这些事件,就算系统处于待机状态,同样不会影响运行

  • Android编程实现闹钟的方法详解

    本文实例讲述了Android编程实现闹钟的方法.分享给大家供大家参考,具体如下: 在Android中,有一个闹钟的类,AlarmManager类,我们可以通过其来对系统内的通知进行操作! 本文用到了Service和Broadcast两个后台管理,在处理时,曾出现过很多问题,但经过半天的努力,终于解决了! 首先是Main.xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns

  • android 设置闹钟及通知示例

    简单说一下这次demo内容,首先做一个设置一次性闹钟,先得到alarmManager,打开一个时间对话框,在里面设置闹钟的时间,时间一到发送广播,然后广播接受者接到跳转到新的activity播放音乐.接着是一个反复闹钟,最后是一个简单的通知,具体代码如下: import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationManager; import android.

  • Android编程实现PendingIntent控制多个闹钟的方法

    本文实例讲述了Android编程实现PendingIntent控制多个闹钟的方法.分享给大家供大家参考,具体如下: 要用 android.app.PendingIntent.getBroadcast(Context context, int requestCode, Intent intent)来实现控制多个闹钟,关键点在于其中的一个参数requestCode. 举例说明如下: public void setClock(){ if(lva.ids.equals("")||lva.ids

  • Android编程闹钟设置方法详解

    本文实例讲述了Android编程闹钟设置方法.分享给大家供大家参考,具体如下: 闹钟在生活中最常见了,在Android中可以通过AlarmManager来实现闹钟,AlarmManager类专门用来设置在某个指定的时间去完成指定的时间.AlarmManager就会通过onReceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行.可以通过Context.getSystemService方法来获得该服务.AlarmManager中的方法不少,如下: 方法 说明 Cancel 取消

  • Android实现简易闹钟功能

    本文实例为大家分享了Android通过广播来实现闹钟的具体代码,供大家参考,具体内容如下 1.创建广播接收RepeatingAlarm.java import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class RepeatingAlarm extends BroadcastRe

  • Android闹钟设置的解决方案

    Android设置闹钟并不像IOS那样这么简单,做过Android设置闹钟的开发者都知道里面的坑有多深.下面记录一下,我解决Android闹钟设置的解决方案. 主要问题 1.API19开始AlarmManager的机制修改. 2.应用程序被Kill掉后,设置的闹钟不响. 3.6.0以上进入Doze模式会使JobScheduler停止工作. 4.手机设置重启后,闹钟失效问题. API19以上AlarmManager机制的修改 API19之前AlarmManager提供了三个设置闹钟的方法,由于业务

随机推荐