Android开发Service在背后每隔1秒执行操作
关键代码:
/**
* 设置定时器,准备下次调用
*
* @param time 调用时间,秒
*/
private void next(int time) {
Intent intent = new Intent(this, VersionCheckServices.class);
//pendingIntent = PendingIntent.getService(this, 0, intent, 0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
manager.set(AlarmManager.RTC, System.currentTimeMillis() + time * 1000, pendingIntent);
}
我的项目示例代码:
/**
* author:congge
* date: 2019/4/18 10:40
* desc:审核版本每隔1分钟请求一次接口
*
**/
public class VersionCheckServices extends Service {
private static String TAG = VersionCheckServices.class.getName();
/**
* 定时器对象
*/
private AlarmManager manager;
private PendingIntent pendingIntent;
private final int LOOP_TIME = 60;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
}
@Override
public void onDestroy() {
if (pendingIntent != null) {
manager.cancel(pendingIntent);
}
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!SPNoCleanUtil.getBoolean(SPKey.CHECK_SAVE,false) && !SPUtils.isCheckUser()) {
if (!Utils.isForeground(this)) {
new CheckVersionPassPresenter().dealCheckVersion(checkVersionCallBack);
} else {
next(LOOP_TIME);
}
} else {
//非审核版本就不需要继续调用服务了
stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
/**
* 检查审核版本的回调
*/
private HttpOnNextListener checkVersionCallBack = new HttpOnNextListener<CheckPassVersionResult>() {
@Override
public void onError(ApiException e) {
next(LOOP_TIME);
}
@Override
public void onNext(CheckPassVersionResult checkPassVersionResult) {
if(checkPassVersionResult.getData() != null && checkPassVersionResult.getData().isPass()){
SPNoCleanUtil.saveCheckVersion(true);
IntentUtils.startGuideActivity(VersionCheckServices.this);
} else {
next(LOOP_TIME);
SPNoCleanUtil.saveCheckVersion(false);
}
}
@Override
public void onDealCodeNext(CheckPassVersionResult baseResult) {
next(LOOP_TIME);
SPNoCleanUtil.saveCheckVersion(false);
}
};
/**
* 设置定时器,准备下次调用
*
* @param time 调用时间,秒
*/
private void next(int time) {
Intent intent = new Intent(this, VersionCheckServices.class);
//pendingIntent = PendingIntent.getService(this, 0, intent, 0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
manager.set(AlarmManager.RTC, System.currentTimeMillis() + time * 1000, pendingIntent);
}
}
应该你们都看得懂,把需求代码换成你们的即可。
调用:
//获取最新是否是审核版本
if ( ModifySetUtils.isCheckVersion()) {
startService(new Intent(mContext, VersionCheckServices.class));
}
还有问题的可评论或者私信我。