Android开发之定时任务的创建

android.content.Context

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

与应用程序环境的全局信息的接口。 这是一个抽象类,它的实现由android系统提供。 它允许访问特定于应用程序的资源和类,以及用于应用程序级操作(例如启动活动,广播和接收意图等)的呼叫。



android.app.AlarmManager



int type:闹钟的类型,常用的有5个值:AlarmManager.ELAPSED_REALTIMEAlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。

AlarmManager.ELAPSED_REALTIME表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为3;

AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2;

AlarmManager.RTC表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为1;

AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0;

AlarmManager.POWER_OFF_WAKEUP表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为4;不过本状态好像受SDK版本影响,某些版本并不支持;


android.os.SystemClock



android.app.PendingIntent

pendingIntent字面意义:等待的,未决定的Intent。要得到一个pendingIntent对象,使用方法类的静态方发类getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int)  分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等等。



Intent intent=new Intent(this,NextActivity.class);

PendingIntent pendIntent=PendingIntent.getActivity(this,0,intent,0);

AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);

long triggerAtTime=SystemClock.elapsedRealTime()+10*1000;//10秒后执行

alarmManager.set(ELAPSED_REALTIME,triggerAtTime,pendingIntent);



/**后台服务中的定时任务*/

public class LongRunningService extends Service{

@Override

public IBinder onBinder(Intent intent){

return null;

}

@Override

public int onStartCommand(Intent intent,int flags,int startId){

new Thread(new Runnabel{

@Override

public void run(){

}

});

Intent intent=new Intent(this,LongRunningService.class);

PendingIntent pendIntent=PendingIntent.getService(this,0,intent,0);

AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);

long triggerAtTime=SystemClock.elapsedRealTime()+10*1000;//10秒后执行

alarmManager.set(ELAPSED_REALTIME,triggerAtTime,pendingIntent);

return super.onStartCommand(intent,flags,startId);

}

}



/**启动服务*/

Intent intent=new Intent(context.LongRunningService.class);

context.startService(intent);



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值