Android实现闹钟功能小Dome

发布时间:2020-10-07 17:11:22 作者:Risk_平安
来源:脚本之家 阅读:383

闹钟闹不醒的可以自己去调整下,这个最是最基本的

MainActivity

public class MainActivity extends AppCompatActivity {

  private AlarmManager alarmManager;
  private PendingIntent pendingIntent;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取闹钟管理者
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);


  }

  public void setAlarmOne(View view) {
    //获取当前系统时间
    Calendar calendar = Calendar.getInstance();
    int huor = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);


    //1 弹出时间对话框
    TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

      @Override
      public void onTimeSet(TimePicker view, int i, int i1) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, i);
        c.set(Calendar.MINUTE, i1);
        //2确定好选择的时间
        //3设置闹钟
        //4当时间一到,将执行的响应
        Intent intent=new Intent();
        intent.setAction("com.zking.risk_android_alarn_notification.RING");
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
        alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pendingIntent);
      }
    }, huor, minute, true);
    timePickerDialog.show();
  }

  public void setAlarmCycle(View view) {
    //获取当前系统时间
    Calendar calendar=Calendar.getInstance();
    int hour=calendar.get(Calendar.HOUR_OF_DAY);
    int minute=calendar.get(Calendar.MINUTE);

    //01.弹出时间对话框
    TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
      @Override
      public void onTimeSet(TimePicker timePicker, int i, int i1) {
        Calendar c=Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,i);
        c.set(Calendar.MINUTE,i1);
        //02.确定好选择的时间
        //03.设置闹钟

        //04.当之间一到,将执行的响应
        Intent intent=new Intent();
        intent.setAction("com.zking.risk_android_alarn_notification.RING");
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x102,intent,0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);
      }
    },hour,minute,true);
    timePickerDialog.show();

  }

  public void cancelAlarmCycle(View view){
    alarmManager.cancel(pendingIntent);
  }


}

RingActivity

public class RingActivity extends AppCompatActivity {

  private MediaPlayer mediaPlayer;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ring);

    //播放音乐
    mediaPlayer = MediaPlayer.create(this, R.raw.rooster);
    mediaPlayer.start();
  }

  public void close(View view) {
    mediaPlayer.stop();
    finish();
  }
}

RingReceived

public class RingReceived extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
    if("com.zking.risk_android_alarn_notification.RING".equals(intent.getAction())){
      Log.i("test","收到广播了");

      //跳转到Activity
      Intent intent1=new Intent(context,RingActivity.class);
      //给Intent设置标志位Flag
      intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      //Intent的组成部分:ComponementName extra Action Data Categlay(分组) Flag
      context.startActivity(intent1);
    }
  }
}

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
  android:layout_width="match_parent" android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  tools:context="com.zking.risk_android_alarn_notification.MainActivity">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置闹钟(一次)"
    android:onClick="setAlarmOne"
    />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置闹钟(周期)"
    android:onClick="setAlarmCycle"
    />
<Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="取消周期闹钟"
  android:onClick="cancelAlarmCycle"
  />

</LinearLayout>

actiity_ring

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_ring"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  tools:context="com.zking.risk_android_alarn_notification.RingActivity"
  android:weightSum="1">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="亲爱的,该起床了"
    android:textSize="30sp"
    />

  <TimePicker
    android:layout_width="324dp"
    android:layout_height="378dp"
    android:id="@+id/timePicker"
    android:layout_weight="0.30" />

<Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="关闭"
  android:onClick="close"
  />


</LinearLayout>

最后的清单配置

 <activity android:name=".RingActivity"></activity>

    <receiver android:name=".RingReceived">
      <intent-filter>
        <action android:name="com.zking.risk_android_alarn_notification.RING"></action>
      </intent-filter>

    </receiver>

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

推荐阅读:
  1. Android实现闹钟小程序
  2. 如何在Android中通过AlarmManager类实现闹钟功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 闹钟 roi

上一篇:jQuery仿移动端支付宝键盘的实现代码

下一篇:list转tree和list中查找某节点下的所有数据操作

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》