android采用BroadcastReceiver实现定时器

package net.blogjava.mobile;

import java.util.Calendar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver
{
	private static int num = 0;  
	@Override
	public void onReceive(Context context, Intent intent)
	{
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"alarm_record", Activity.MODE_PRIVATE);
		String hour = String.valueOf(Calendar.getInstance().get(
				Calendar.HOUR_OF_DAY));
		String minute = String.valueOf(Calendar.getInstance().get(
				Calendar.MINUTE));
		Log.e("ActionBroadCast", "New Message !" + num++);
		// 从XML 文件夹获得描述当前时间点的value
		String time = sharedPreferences.getString(hour + ":" + minute, null);
		if (time != null) {
			// 播放声音
			MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.ring);
			mediaPlayer.start();
		}

	}
}



package net.blogjava.mobile;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class Main extends Activity implements OnClickListener
{
	private TextView tvAlarmRecord;
	private SharedPreferences sharedPreferences;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button btnAddAlarm = (Button) findViewById(R.id.btnAddAlarm);
		tvAlarmRecord = (TextView) findViewById(R.id.tvAlarmRecord);
		btnAddAlarm.setOnClickListener(this);
		sharedPreferences = getSharedPreferences("alarm_record", Activity.MODE_PRIVATE);

		AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(this, AlarmReceiver.class);
		//创建封装BroadcastReceiver 的pendingIntent对象
		PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,intent, 0);
		//开始定时器,每1分钟执行一次,5s
		alarmManager.setRepeating(AlarmManager.RTC, 0, 30000, pendingIntent);
		//alarmManager.cancel(pendingIntent);
	}	
	
	@Override
	public void onClick(View v)
	{
		View view = getLayoutInflater().inflate(R.layout.alarm, null);

		final TimePicker timePicker = (TimePicker) view.findViewById(R.id.timepicker);
		timePicker.setIs24HourView(true);
		
		// 显示设置时间点的对话框
		new AlertDialog.Builder(this).setTitle("设置提醒时间").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						String timeStr = String.valueOf(timePicker.getCurrentHour())
								+ ":"
								+ String.valueOf(timePicker.getCurrentMinute());
						//将时间点添加到  TextView  组件中
						tvAlarmRecord.setText(tvAlarmRecord.getText().toString() + "\n" + timeStr);
						//count++;
						
						/*View view2 = new View(Main.this);
						view2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
						view2.setBackgroundColor(Color.RED);
						LinearLayout linearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.main, null);
						linearLayout.addView(view2);	*/
						//保存时间点
						sharedPreferences.edit().putString(timeStr, timeStr).commit();
					}
				}).setNegativeButton("取消", null).show();
	}
}


你可能感兴趣的:(日企项目)