您的位置:首页 > 其它

AlarmManager的使用

2011-02-24 10:10 267 查看
在Android中有一个Alarm服务。假如要在未来的某个时间点执行一个任务或者重复执行某个任务。可以使用AlarmManager来实现。比如说闹钟。本节主要编写了一个AlarmManager的使用示例。

本节包含一个Activity和两个Receiver。

Activity如下:

package com.xiaochun91103;

import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AlarmController extends Activity {
/** Called when the activity is first created. */
private Button bt1,bt2,bt3;
private Toast toast;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

bt1 = (Button)findViewById(R.id.bt1);
bt2 = (Button)findViewById(R.id.bt2);
bt3 = (Button)findViewById(R.id.bt3);

bt1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AlarmController.this,ExecuteOnce.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
if(toast != null) {
toast.cancel();
}
toast = Toast.makeText(AlarmController.this, "10秒钟后执行一次Alarm",
Toast.LENGTH_LONG);
toast.show();
}
});

bt2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AlarmController.this,RepeatingAlarm.class);
long now = SystemClock.elapsedRealtime();
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
now = now+5*1000;
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
now, 5*1000, sender);
if(toast != null) {
toast.cancel();
}
toast = Toast.makeText(AlarmController.this, "每5秒重复执行",
Toast.LENGTH_SHORT);
toast.show();
}
});

bt3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
if(toast != null) {
toast.cancel();
}
toast = Toast.makeText(AlarmController.this, "结束Alarm",
Toast.LENGTH_SHORT);
toast.show();
}

});
}
}


Receiver用来显示一个提示信息:

package com.xiaochun91103;

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

public class ExecuteOnce extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "本次任务执行一次", Toast.LENGTH_SHORT).show();
}

}


package com.xiaochun91103;

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

public class RepeatingAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "本次任务重复执行", Toast.LENGTH_SHORT).show();
}

}


程序运行结果截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: