android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动
android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动
1、先简单设置一个闹钟提醒事件:
//设置闹钟 mSetting.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub calendar.setTimeInMillis(System.currentTimeMillis()); int mHour=calendar.get(Calendar.HOUR_OF_DAY); int mMinute=calendar.get(Calendar.MINUTE); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener(){ public void onTimeSet(TimePicker view,int hourOfDay, int minute){ // 设置时间 calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY,hourOfDay); calendar.set(Calendar.MINUTE,minute); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); //广播跳转 Intent intent = new Intent(MainActivity.this, CallAlarmReceiver.class); //启动一个广播 PendingIntent sender=PendingIntent.getBroadcast( MainActivity.this,0, intent, 0); //创建闹钟 AlarmManager am; am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),sender); String tmpS=format(hourOfDay)+":"+format(minute); mDate.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(); } });
注意:CallAlarmReceiver 广播接收器,所以要在AndroidManifest.xml配置:如
<receiver android:name=".CallAlarmReceiver" android:process=":remote" />
2、广播接收器:
很简单一个继承BroadcastReceiver,然后跳转到弹框的Activity
public class CallAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Intent alaramIntent = new Intent(context, AlarmSettingAgain.class); alaramIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(alaramIntent); } }
3、接收后处理,包括弹出dialog,播放铃声,震动和锁屏状态下亮屏
(1)、首先要在onCreate()上加一句,确保在锁屏状态下弹出Dialog不会出错。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); // hide title Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); winParams.flags |= (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); startMedia(); startVibrator(); createDialog(); }
(2)、锁屏状态下唤醒屏幕,要在onResume() 方法中启动,并且要在onPause()中释放,否则会出错。
@Override protected void onResume() { super.onResume(); // 唤醒屏幕 acquireWakeLock(); } @Override protected void onPause() { super.onPause(); releaseWakeLock(); } /** * 唤醒屏幕 */ private void acquireWakeLock() { if (mWakelock == null) { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass() .getCanonicalName()); mWakelock.acquire(); } } /** * 释放锁屏 */ private void releaseWakeLock() { if (mWakelock != null && mWakelock.isHeld()) { mWakelock.release(); mWakelock = null; } }
(3)、播放铃声:
/** * 开始播放铃声 */ private void startMedia() { try { mp.setDataSource(this, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)); //铃声类型为默认闹钟铃声 mp.prepare(); mp.start(); } catch (Exception e) { e.printStackTrace(); } }
(4)、震动:
/** * 震动 */ private void startVibrator() { /** * 想设置震动大小可以通过改变pattern来设定,如果开启时间太短,震动效果可能感觉不到 * */ vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); long[] pattern = { 500, 1000, 500, 1000 }; // 停止 开启 停止 开启 vibrator.vibrate(pattern, 0); }
(5)、弹出Dialog对话框
private void createDialog() { new AlertDialog.Builder(this) .setIcon(R.drawable.clock) .setTitle("闹钟提醒时间到了!!") .setMessage("你使用闹钟时间到了!!!") .setPositiveButton("推迟10分钟", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { tenMRemind(); mp.stop(); vibrator.cancel(); finish(); } }) .setNegativeButton("关闭", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { mp.stop(); vibrator.cancel(); finish(); } }).show(); } /** * 推迟10分钟提醒 */ private void tenMRemind(){ //设置时间 Calendar calendar_now =Calendar.getInstance(); calendar_now.setTimeInMillis(System.currentTimeMillis()); calendar_now.set(Calendar.HOUR_OF_DAY, calendar_now.get(Calendar.HOUR_OF_DAY)); calendar_now.set(Calendar.MINUTE, calendar_now.get(Calendar.MINUTE)+10); calendar_now.set(Calendar.SECOND, 0); calendar_now.set(Calendar.MILLISECOND, 0); //时间选择好了 Intent intent = new Intent(this, CallAlarmReceiver.class); //注册闹钟广播 PendingIntent sender = PendingIntent.getBroadcast( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am; am = (AlarmManager) this.getSystemService(this.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar_now.getTimeInMillis(), sender); }
(6)、因为震动和锁屏都是系统级的,所以要在AndroidManifest.xml加上权限
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK"/>