系统服务详解之时间服务3

简介: 4、全局定时器:AlermManager 全局定时器是与应用程序独立的系统时间服务,并不依赖应用程序而存在。 该对象通过AlarmManage alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);获得。

4、全局定时器:AlermManager

全局定时器是与应用程序独立的系统时间服务,并不依赖应用程序而存在。

该对象通过

AlarmManage alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
获得。

通过PendingIntent指定一个Activity,再通过setRepeating方法设置定时器

                                intent = new Intent(this, MyActivity.class);
				pendingActivityIntent = PendingIntent.getActivity(this, 0,
						intent, 0);				
				alarmManager.setRepeating(AlarmManager.RTC, 0, 5000,
						pendingActivityIntent);

通过全局定时器,我们来演示一个每5秒更换手机壁纸的程序:

更换壁纸的Service类:

public class ChangeWallpaperService extends Service
{
	private static int index = 0;
	private int[] resIds = new int[]
	{ R.raw.wp1, R.raw.wp2, R.raw.wp3, R.raw.wp4, R.raw.wp5

	};
	

	@Override
	public void onStart(Intent intent, int startId)
	{

		if(index == 5)
			index = 0;
		InputStream inputStream = getResources().openRawResource(resIds[index++]);
		try
		{
			setWallpaper(inputStream);
		}
		catch (Exception e)
		{

		}
		super.onStart(intent, startId);
	}

	@Override
	public void onCreate()
	{
		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent)
	{
		// TODO Auto-generated method stub
		return null;
	}
在mainfirst中配置

	<uses-permission android:name="android.permission.SET_WALLPAPER" />

全局定时器:

public class Main extends Activity implements OnClickListener
{
	private Button btnStart;
	private Button btnStop;
	@Override
	public void onClick(View view)
	{
		AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		PendingIntent pendingIntent = PendingIntent.getService(this, 0,
				new Intent(this, ChangeWallpaperService.class), 0);
		switch (view.getId())
		{
			case R.id.btnStart:
				alarmManager.setRepeating(AlarmManager.RTC, 0, 5000,
						pendingIntent);				
				btnStart.setEnabled(false);
				btnStop.setEnabled(true);
				break;

			case R.id.btnStop:
				alarmManager.cancel(pendingIntent);
				btnStart.setEnabled(true);
				btnStop.setEnabled(false);
				break;
		}
	}
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btnStart = (Button) findViewById(R.id.btnStart);
		btnStop = (Button) findViewById(R.id.btnStop);
		btnStop.setEnabled(false);
		btnStart.setOnClickListener(this);
		btnStop.setOnClickListener(this);
	}
}

在创建PendingIntent对象时与service绑定,那么AlarmManager在执行时就会执行service的onStart方法(不是onCreate方法,因为他只会执行一次),获得Service的PendingIntent对象是使用getService方法,同理getActivity,getBroadcast。

下面演示另一个全局定时器的例子,本例是实现多次定时提醒功能。

定时提醒的原理:在添加时间节点后,需要将所添加的时间节点保存在文件或数据库中。本例使用前面介绍过的SharedPreferances来保存。然后使用AlarmManager每隔一分钟扫描一次,在扫描过程中获得当前时间的value,如果成功则说明当前时间为时间点,否则继续扫描。

本例使用BroadcastReceiver来处理定时提醒任务:

public class AlarmReceiver extends BroadcastReceiver
{

	@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));
		String time = sharedPreferences.getString(hour + ":" + minute, null);
		if (time != null)
		{			
			MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.ring);
			mediaPlayer.start();
		}
		
	}

}

配置文件:

	<receiver android:name=".AlarmReceiver" android:enabled="true" />
主程序中每添加一个时间点就会在XML文件中保存所添加的时间点:
public class Main extends Activity implements OnClickListener
{
	private TextView tvAlarmRecord;
	private SharedPreferences sharedPreferences;

	@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());
						tvAlarmRecord.setText(tvAlarmRecord.getText()
								.toString()
								+ "\n" + timeStr);
						sharedPreferences.edit().putString(timeStr, timeStr)
								.commit();

					}
				}).setNegativeButton("取消", null).show();
	}

	@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);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
				intent, 0);
		alarmManager
				.setRepeating(AlarmManager.RTC, 0, 60 * 1000, pendingIntent);

	}
}



目录
相关文章
|
1月前
|
调度 Windows Perl
进程和计划任务管理
进程和计划任务管理
18 0
|
7月前
|
调度 Windows Perl
进程和计划任务管理命令
进程和计划任务管理命令
62 0
|
安全 调度 监控
进程和计划任务管理(4)
内核的功用:进程管理、内存管理、文件系统、网络功能、驱动程序、安全功能等。 1 程序 1.1 什么是程序? 是一组计算机能识别和执行的指令,运行于电子计算机上,满足人们某种需求的信息化工具。 用于描述进程要完成的功能,是控制进程执行的指令集。 保存在硬盘、光盘等介质中的可执行代码和数据。 静态保存的代码。
98 1
|
缓存 安全 调度
进程和计划任务管理(2)
内核的功用:进程管理、内存管理、文件系统、网络功能、驱动程序、安全功能等。 1 程序 1.1 什么是程序? 是一组计算机能识别和执行的指令,运行于电子计算机上,满足人们某种需求的信息化工具。 用于描述进程要完成的功能,是控制进程执行的指令集。 保存在硬盘、光盘等介质中的可执行代码和数据。 静态保存的代码。
84 1
|
监控 安全 Unix
进程和计划任务管理(1)
内核的功用:进程管理、内存管理、文件系统、网络功能、驱动程序、安全功能等。 1 程序 1.1 什么是程序? 是一组计算机能识别和执行的指令,运行于电子计算机上,满足人们某种需求的信息化工具。 用于描述进程要完成的功能,是控制进程执行的指令集。 保存在硬盘、光盘等介质中的可执行代码和数据。 静态保存的代码。
96 0
|
安全 Linux Ubuntu
进程和计划任务管理(3)
内核的功用:进程管理、内存管理、文件系统、网络功能、驱动程序、安全功能等。 1 程序 1.1 什么是程序? 是一组计算机能识别和执行的指令,运行于电子计算机上,满足人们某种需求的信息化工具。 用于描述进程要完成的功能,是控制进程执行的指令集。 保存在硬盘、光盘等介质中的可执行代码和数据。 静态保存的代码。
106 1
|
运维 监控 安全
精简系统服务和开机进程| 学习笔记
快速学习精简系统服务和开机进程。
127 0
|
运维 监控 安全
精简系统服务和开机进程
一、线上服务器建议开启的服务 二、可删除的系统用户和组 三、下期公开课预告 四、广而告之