Big Bug Ban

兴趣 践行 创新

Android开发日志 闹钟

 

近期要做个东西,需要设计到闹钟的设置.

现在把遇到的问题和详细步骤的给大家分享下~

首先 闹钟是位于ALARM_SERVICE用AlarmManager进行管理

AlarmManager am = (AlarmManager)getSystemService();

用这一句就可以直接得到闹钟管理

然后你就可以设置闹钟了.

am.setRepeating(type, triggerAtTime, interval, operation)

或者

am.set(type, triggerAtTime, operation)           

type的话用AlarmManager.RTC_WAKEUP或者其他,参考SDK文档~

operation是一个PendingIntent

之所以要这个..是因为我们需要在程序退出后.时间到了也可以正常被系统调入然后执行

创建一个类继承BroadcastReceiver

/**
 * Copyright 2010 princehaku
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * User: princehaku
 * Date: 11-6-12
 * Time: 上午11:18
 */
package net.techest.alarm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {

	
	@Override
	public void onReceive(Context context, Intent intent) {
		
		Log.i("AR", "Time Up");

	}
}

然后onReceive上面就是闹钟响的时候执行的代码

还有就是这个类必须在mainfest.xml里面注册成监听

<receiver android:name=".AlarmReceiver" android:process=":remote" />

然后就可以使用了~

Written by princehaku

6月 12th, 2011 at 10:04 下午

Posted in Android

Tagged with

without comments

Leave a Reply