写点什么

android Alarm 闹钟发送广播播放音乐,【大牛疯狂教学

用户头像
Android架构
关注
发布于: 10 小时前

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);


//根据当前时间预设一个警报


am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);


/**


  • 第一个参数是警报类型;第二个参数是第一次执行的延迟时间,可以延迟,也可以马上执行;第三个参数是重复周期为一天

  • 这句话的意思是设置闹铃重复周期,也就是执行警报的间隔时间


*/


// am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(60*1000),


// (246060*1000), pi);


// am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),


// 1000605, pi);


String msg = hourOfDay+":"+minute;


tv.setText("当前设置的闹钟时间:"+msg);


}


}, hour, minute, true).show();


//上面的 TimePickerDialog 中的 5 个参数参考:http://blog.csdn.net/yang_hui1986527/article/details/6839342


}


});


cancelTime.setOnClickListener(new Button.OnClickListener()


{


@Override


public void onClick(View v) {


Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);


PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,


intent, 0);


AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);


//取消警报


am.cancel(pi);


tv.setText("闹钟取消");


//取消闹钟的同时取消音乐


stopService(new Intent("com.yqy.yqy_alarm.MUSIC"));


}


});


}


}


AlarmReceiver


package com.yqy.yqy_alarm;


import android.app.Notification;


import android.app.NotificationManager;


import android.app.PendingIntent;


import android.content.BroadcastReceiver;


import android.content.ComponentName;


import android.content.Context;


import android.content.Intent;


import android.widget.Toast;


public class AlarmReceiver extends BroadcastReceiver {


Intent intent;


int requestCode = -1;


@Override


public void onReceive(Context context, Intent arg1) {


requestCode = arg1.getIntExtra("requestCode", 0);


NotificationManager nm = (NotificationManage


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


r)context.getSystemService(Context.NOTIFICATION_SERVICE);


Notification n = new Notification(R.drawable.ic_launcher, "Hello,there!", System.currentTimeMillis());


n.flags = Notification.FLAG_AUTO_CANCEL;


intent = new Intent(context, MainActivity.class);


intent.putExtra("alarm", "alarm");


intent.setComponent(new ComponentName("com.yqy.yqy_alarm", "com.yqy.yqy_alarm.MainActivity"));


intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步,设置启动模式


PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);


n.setLatestEventInfo(


context,


"Hello,there!",


"Hello,there,I'm john.",


contentIntent);


nm.notify(R.string.app_name, n);


Toast.makeText(context, "闹钟时间到", Toast.LENGTH_SHORT).show();


intent = new Intent("com.yqy.yqy_alarm.MUSIC");


//启动 Service 播放音乐


context.stopService(new Intent("com.yqy.yqy_alarm.MUSIC"));


context.startService(intent);


}


}


MusicService


package com.yqy.yqy_alarm;


import android.app.Service;


import android.content.Intent;


import android.media.MediaPlayer;


import android.os.IBinder;


public class MusicService extends Service {


private MediaPlayer player;


@Override


public IBinder onBind(Intent intent) {


// TODO Auto-generated method stub


return null;


}


public void onStart(Intent intent, int startId)


{


super.onStart(intent, startId);


player = MediaPlayer.create(this, R.raw.beep);


player.setLooping(true);


player.start();


}


public void onDestroy()


{


super.onDestroy();


if(player != null){


player.stop();


}

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
android Alarm闹钟发送广播播放音乐,【大牛疯狂教学