android之闹钟

不逼逼,直接上代码


我这里是用广播来实现闹钟效果的

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;
    private PendingIntent pendingIntent;

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

    }
    //开启一次性闹钟的onClick事件
    public void steatone(View view){
        //获取系统当前时间
        Calendar calendar=Calendar.getInstance();
        int h=calendar.get(Calendar.HOUR_OF_DAY);
        int m=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);
                //确定好选择的时间
                //设置闹钟

                //04.当之间一到,将执行的响应
                Intent intent=new Intent();
                intent.setAction("唯一性名字");
                PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);

            }
        },h,m,true);
        timePickerDialog.show();
    }


    //开启周期性闹钟的onClick事件
    public void steattwo(View view){
        //获取系统当前时间
        Calendar calendar=Calendar.getInstance();
        int h=calendar.get(Calendar.HOUR_OF_DAY);
        int m=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);
                //确定好选择的时间
                //设置闹钟

                //04.当之间一到,将执行的响应
                Intent intent=new Intent();
                intent.setAction("唯一性名字");
                PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);

            }
        },h,m,true);
        timePickerDialog.show();
    }
    //关闭所有闹钟闹钟的onClick事件
    public void stop(View view){
        alarmManager.cancel(pendingIntent);
    }

}


广播接受者


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by YaozzzzMABY on 2017/2/22.
 */

public class RingReceived extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if("唯一性名字".equals(intent.getAction())){
            Log.i("test","收到广播了");
            //跳转到Activity
            Intent intent1=new Intent(context,TempActivity.class);
            //给Intent设置标志位Flag
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //Intent的组成部分:ComponementName extra  Action Data Categlay(分组) Flag
            context.startActivity(intent1);
        }


    }
}


如若闹钟激活则跳转到


import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class TempActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temp);
//播放音乐        
mediaPlayer = MediaPlayer.create(this, R.raw.ss1);
        mediaPlayer.start();


    }
   //关闭按钮的事件
    public void close(View view){
        mediaPlayer.stop();
        finish();
    }

}



具体代码大部分都在这,有鲜明的注释,不交换懂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值