Android之实现简单小闹钟

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

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

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

      3、注册广播接收器

布局文件(一次性闹钟)

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.pxd.alarmandnotice.MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置一次性闹钟"
        android:onClick="alarmOne"
        />
</LinearLayout>
MainActivity.java
package com.pxd.alarmandnotice;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    private AlarmManager alarmManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取闹钟的管理者
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    }
    public  void  alarmOne(View view){
        //获取当前系统的时间
        Calendar calendar=Calendar.getInstance();
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        int minute=calendar.get(Calendar.MINUTE);
        //弹出时间对话框
        TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Calendar c=Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY,hourOfDay);
                c.set(Calendar.MINUTE,minute);
                //时间一到,发送广播(闹钟响了)
                Intent intent=new Intent();
                intent.setAction("com.pxd.alarmandnotice.RING");
                //将来时态的跳转
                PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
              //设置闹钟
                alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
       }
        },hour,minute,true);
        timePickerDialog.show();
    }
}

广播接受者:RingReceived
package com.pxd.alarmandnotice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class RingReceived extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if("com.pxd.alarmandnotice.RING".equals(intent.getAction())){
            Log.i("test","闹钟响了");
            //跳转到Activity n //广播接受者中(跳转Activity)
            Intent intent1=new Intent(context,RingActivity.class);
            //给Intent设置标志位
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent1);
        }
    }
}

要跳转的Activity,RingActivity播放音乐
package com.pxd.alarmandnotice;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class RingActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ring);
      //时间一到跳转Activity,在这个Activity中播放音乐
        mediaPlayer = MediaPlayer.create(this, R.raw.one);
        mediaPlayer.start();
    }
    public void stop(View view){
        mediaPlayer.stop();
        finish();
    }

}

响铃闹钟布局,activity_ring.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="起床了!!!"
        android:textSize="30sp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止"
        android:onClick="stop"
        />
</LinearLayout>

实现周期性闹钟只需把
 //设置闹钟
 alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent)
改为:我这里设置的是5秒响一次
 //设置周期闹钟(5秒)
  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);
       
//停止闹钟
public void cancelMany(View view){
    alarmManager.cancel(pendingIntent);
}
这样一个简单的小闹钟就可以实现了。







展开阅读全文

没有更多推荐了,返回首页