Android闹钟-AlarmManager

AlarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服务。AlarmManager对象一般通过Context.getSystemService(Context.ALARM_SERVICE)方法获得。


下面看一个例子加深理解:

package com.app;

import com.app.R;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 
 * 测试AlarmManager
 */
public class MainActivity extends Activity {
	// 声明Button
	private Button setBtn, cancelBtn;
	// 定义广播Action
	private static final String BC_ACTION = "com.action.BC_ACTION";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置当前布局视图
		setContentView(R.layout.main);
		
		// 实例化Button
		setBtn = (Button) findViewById(R.id.Button01);
		cancelBtn = (Button) findViewById(R.id.Button02);
		
		// 获得AlarmManager实例
		final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		
		// 实例化Intent
		Intent intent = new Intent();
		// 设置Intent action属性
		intent.setAction(BC_ACTION);
		intent.putExtra("msg", "你该去开会啦!");
		// 实例化PendingIntent
		final PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,
				intent, 0);
		// 获得系统时间
		final long time = System.currentTimeMillis();

		// 设置按钮单击事件
		setBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 重复提示,从当前时间开始,间隔5秒
				am.setRepeating(AlarmManager.RTC_WAKEUP, time,
						5 * 1000, pi);
			}
		});
		
		// 设置按钮单击事件
		cancelBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				am.cancel(pi);
			}
		});
	}
}

注:PendingIntent的一个静态方法,

getBroadcast

public static PendingIntent getBroadcast(Context context,
                                         int requestCode,
                                         Intent intent,
                                         int flags)
Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().

参数:
context - The Context in which this PendingIntent should perform the broadcast.
requestCode - Private request code for the sender (currently not used).
intent - The Intent to be broadcast.
flags - May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
返回:
Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.


package com.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		// 获得提示信息
		String msg = intent.getStringExtra("msg");
		// 显示提示信息
		Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
	}
}


别忘了在AndroidManifest.xml中注册MyReceiver:
<receiver android:name="MyReceiver">
        <intent-filter>
        	<action android:name="com.action.BC_ACTION"/>
        </intent-filter>
 </receiver>
结果:



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzyjr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值