android 闹钟的简单实现

private AlarmManager am;
private Intent alarmIntent;
private PendingIntent alarm_pi;
private MediaPlayer mp;

am = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(MainActivity.this,ClockReceiver.class);
alarm_pi = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);


Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
am.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), alarm_pi);//设定当前时间触发闹钟


 
  

闹钟触发Receiver

package com.example.an_hour;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;

public class ClockReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Intent i = new Intent(context, AlarmDialog.class);
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(i);
	}

}

receiver接收到alarm信息,打开
AlarmDialog activity
 弹出dialog,播放MP3音乐

package com.example.an_hour;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder.AudioSource;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;

public class AlarmDialog extends Activity {
	private MediaPlayer mp;
	private String time;
	private Calendar calendar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_layout);
		playMusic();
		showAlarmDialog();
	}

			private void showAlarmDialog() {
				calendar = Calendar.getInstance();
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy,MM,dd,EEEE,HH:mm:ss");
		        time = sdf1.format(calendar.getTime());
				AlertDialog.Builder builder = new AlertDialog.Builder(this)
									.setMessage("now is:"+time)
									.setTitle("正点时间到了")
									.setCancelable(true)
									.setPositiveButton("OK", new DialogInterface.OnClickListener() {
										
										@Override
										public void onClick(DialogInterface dialog, int which) {
											mp.stop();
											mp.release();
											dialog.dismiss();
											AlarmDialog.this.finish();
										}
									});
				AlertDialog dialog = builder.create();
				dialog.show();
				
			}
		    
			private void playMusic() {
				//mp = MediaPlayer.create(this, R.raw.a);
				Uri uri = Uri.parse("/storage/sdcard1/kgmusic/download/Claris - 魔法少女小圆.mp3");
				mp = new MediaPlayer();
				mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
				try {
					mp.setDataSource(AlarmDialog.this,uri);
					mp.prepare();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				mp.start();
			}

			@Override
			public boolean onKeyDown(int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					Intent intent = new Intent(this,MainActivity.class);
					startActivity(intent);
				}
				return super.onKeyDown(keyCode, event);
			}
}

PS:播放本地音乐,需添加权限:



你可能感兴趣的:(android 闹钟的简单实现)