服务相关→ServiceUtils

本文介绍了一个用于Android平台的服务操作工具类,提供了启动、停止、绑定等服务管理功能,并附带了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
   
  import android.app.ActivityManager;
  import android.app.ActivityManager.RunningServiceInfo;
  import android.content.Context;
  import android.content.Intent;
  import android.content.ServiceConnection;
   
  import java.util.HashSet;
  import java.util.List;
  import java.util.Set;
   
  /**
  * <pre>
  * author: Blankj
  * blog : http://blankj.com
  * time : 2016/8/2
  * desc : 服务相关工具类
  * </pre>
  */
  public final class ServiceUtils {
   
  private ServiceUtils() {
  throw new UnsupportedOperationException("u can't instantiate me...");
  }
   
  /**
  * 获取所有运行的服务
  *
  * @param context 上下文
  * @return 服务名集合
  */
  public static Set getAllRunningService(Context context) {
  ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  List<RunningServiceInfo> infos = activityManager.getRunningServices(0x7FFFFFFF);
  Set<String> names = new HashSet<>();
  if (infos == null || infos.size() == 0) return null;
  for (RunningServiceInfo info : infos) {
  names.add(info.service.getClassName());
  }
  return names;
  }
   
  /**
  * 启动服务
  *
  * @param context 上下文
  * @param className 完整包名的服务类名
  */
  public static void startService(Context context, String className) {
  try {
  startService(context, Class.forName(className));
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
   
  /**
  * 启动服务
  *
  * @param context 上下文
  * @param cls 服务类
  */
  public static void startService(Context context, Class<?> cls) {
  Intent intent = new Intent(context, cls);
  context.startService(intent);
  }
   
  /**
  * 停止服务
  *
  * @param context 上下文
  * @param className 完整包名的服务类名
  * @return {@code true}: 停止成功<br>{@code false}: 停止失败
  */
  public static boolean stopService(Context context, String className) {
  try {
  return stopService(context, Class.forName(className));
  } catch (Exception e) {
  e.printStackTrace();
  return false;
  }
  }
   
  /**
  * 停止服务
  *
  * @param context 上下文
  * @param cls 服务类
  * @return {@code true}: 停止成功<br>{@code false}: 停止失败
  */
  public static boolean stopService(Context context, Class<?> cls) {
  Intent intent = new Intent(context, cls);
  return context.stopService(intent);
  }
   
  /**
  * 绑定服务
  *
  * @param context 上下文
  * @param className 完整包名的服务类名
  * @param conn 服务连接对象
  * @param flags 绑定选项
  * <ul>
  * <li>{@link Context#BIND_AUTO_CREATE}</li>
  * <li>{@link Context#BIND_DEBUG_UNBIND}</li>
  * <li>{@link Context#BIND_NOT_FOREGROUND}</li>
  * <li>{@link Context#BIND_ABOVE_CLIENT}</li>
  * <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
  * <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
  * </ul>
  */
  public static void bindService(Context context, String className, ServiceConnection conn, int flags) {
  try {
  bindService(context, Class.forName(className), conn, flags);
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
   
  /**
  * 绑定服务
  *
  * @param context 上下文
  * @param cls 服务类
  * @param conn 服务连接对象
  * @param flags 绑定选项
  * <ul>
  * <li>{@link Context#BIND_AUTO_CREATE}</li>
  * <li>{@link Context#BIND_DEBUG_UNBIND}</li>
  * <li>{@link Context#BIND_NOT_FOREGROUND}</li>
  * <li>{@link Context#BIND_ABOVE_CLIENT}</li>
  * <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
  * <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
  * </ul>
  */
  public static void bindService(Context context, Class<?> cls, ServiceConnection conn, int flags) {
  Intent intent = new Intent(context, cls);
  context.bindService(intent, conn, flags);
  }
   
  /**
  * 解绑服务
  *
  * @param context 上下文
  * @param conn 服务连接对象
  */
  public static void unbindService(Context context, ServiceConnection conn) {
  context.unbindService(conn);
  }
   
  /**
  * 判断服务是否运行
  *
  * @param context 上下文
  * @param className 完整包名的服务类名
  * @return {@code true}: 是<br>{@code false}: 否
  */
  public static boolean isServiceRunning(Context context, String className) {
  ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  List<RunningServiceInfo> infos = activityManager.getRunningServices(0x7FFFFFFF);
  if (infos == null || infos.size() == 0) return false;
  for (RunningServiceInfo info : infos) {
  if (className.equals(info.service.getClassName())) return true;
  }
  return false;
  }
  }
package com.example.demo.service.impl; import com.alibaba.fastjson2.JSONObject; import com.example.demo.dao.BenefitAdminDao; import com.example.demo.dao.IdentityAdminDao; import com.example.demo.dao.IdentityBenefitRelationDao; import com.example.demo.entity.*; import com.example.demo.service.IdentityBenefitAdminService; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.update.UpdateChain; import io.micrometer.common.util.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.*; import java.util.function.Consumer; import java.util.stream.Collectors; @Service public class IdentityBenefitAdminServiceImpl implements IdentityBenefitAdminService { @Autowired private BenefitAdminDao benefitAdminDao; @Autowired private IdentityAdminDao identityAdminDao; @Autowired private IdentityBenefitRelationDao identityBenefitRelationDao; @Override public ResultPage<BenefitInfo> listBenefit(ArguBenefitQuery query) { QueryWrapper wrapper = QueryWrapper.create() .select("b.id", "b.code", "b.name","b.description") .from("benefit").as("b") .leftJoin("identity_benefit_relation").as("ibr") .on("b.code = ibr.benefitCode") .leftJoin("identity").as("i") .on("i.id = ibr.idIdentity") .where("i.isDeleted = FALSE") .and("ibr.isDeleted = FALSE"); if (StringUtils.isNotBlank(query.getIdentityName())) { wrapper.and((Consumer<QueryWrapper>) w -> w.like("i.name", query.getIdentityName())); } if (StringUtils.isNotBlank(query.getBenefitName())) { wrapper.and((Consumer<QueryWrapper>) w -> w.like("b.name", query.getBenefitName())); } wrapper.groupBy("b.id", "b.code", "b.name","b.description"); wrapper.orderBy("b.code"); Page<BenefitInfoSub> benefitInfoSubPage = benefitAdminDao.paginateAs( new Page<>(query.getCurrent(), query.getPageSize()), wrapper, BenefitInfoSub.class ); List<BenefitInfoSub> records = benefitInfoSubPage.getRecords(); List<BenefitInfo> benefitInfoList = records.stream() .map(this::convertToBenefitInfo) .collect(Collectors.toList()); return new ResultPage<>( benefitInfoSubPage.getTotalRow(), benefitInfoSubPage.getPageSize(), benefitInfoSubPage.getPageNumber(), benefitInfoList ); } @Override public ResultPage<IdentityInfo> queryIdentity(ArguIdentityQuery query) { QueryWrapper wrapper = QueryWrapper.create() .select("i.id", "i.name", "i.description") .select("JSON_AGG(JSON_BUILD_OBJECT('name', b.name, 'code', ibr.benefitCode)) AS benefit") .from("identity").as("i") .leftJoin("identity_benefit_relation").as("ibr") .on("i.id = ibr.idIdentity") .leftJoin("benefit").as("b") .on("b.code = ibr.benefitCode") .where("i.isDeleted = FALSE"); if (StringUtils.isNotBlank(query.getIdentityName())) { wrapper.and((Consumer<QueryWrapper>) w -> w.like("i.name", query.getIdentityName())); } wrapper.groupBy("i.id", "i.name", "i.description"); Page<IdentityInfoSub> page = identityAdminDao.paginateAs( new Page<>(query.getCurrent(), query.getPageSize()), wrapper, IdentityInfoSub.class ); List<IdentityInfoSub> records = page.getRecords(); // 将 records 转换为 IdentityInfo List<IdentityInfo> identityInfoList = convertToIdentityInfo(records); // 返回 ResultPage ResultPage<IdentityInfo> resultPage = new ResultPage<>( page.getTotalRow(), page.getPageSize(), page.getPageNumber(), identityInfoList ); return resultPage; } /** * 删除身份 * * @param del * @return */ @Override @Transactional public Boolean delIdentity(ArguDelIdentity del, Long idUser) { LocalDateTime now = LocalDateTime.now(); //删除身份 UpdateChain.of(Identity.class) .set(Identity::getOperator, idUser) .set(Identity::getTimeUpdate, now) .set(Identity::isDeleted, true) .eq(Identity::getId, del.getId()) .update(); //并且删除身份权益关联 UpdateChain.of(IdentityBenefitRelation.class) .set(IdentityBenefitRelation::isDeleted, true) .set(IdentityBenefitRelation::getOperator, idUser) .set(IdentityBenefitRelation::getTimeUpdate, now) .eq(IdentityBenefitRelation::getIdIdentity, del.getId()) .update(); return true; } /** * 修改身份 * * @param update * @return */ @Override @Transactional public Boolean updateIdentity(ArguUpdateIdentity update, Long idUser) { LocalDateTime now = LocalDateTime.now(); UpdateChain.of(Identity.class) .set(Identity::getName, update.getName()) .set(Identity::getDescription, update.getDescription()) .set(Identity::getOperator, idUser) .set(Identity::getTimeUpdate, now) .where(Identity::getId).eq(update.getId()) .update(); List<String> dbBenefitCodes = identityBenefitRelationDao.queryBenefitCodeByIdentityId(update.getId()); List<String> voBenefitCodes = new ArrayList<>(); if (update.getMatchBenefits() != null) for (BenefitItem benefitItem : update.getMatchBenefits()) if (benefitItem.getCode() != null) voBenefitCodes.addAll(Collections.singleton(benefitItem.getCode())); //找出数据库有但入参没有的 benefitCode List<String> dbOnlyBenefitCodes = dbBenefitCodes.stream() .filter(code -> !voBenefitCodes.contains(code)) .collect(Collectors.toList()); if (!dbOnlyBenefitCodes.isEmpty()) for (String code : dbOnlyBenefitCodes) { UpdateChain.of(IdentityBenefitRelation.class) .set(IdentityBenefitRelation::isDeleted, true) .set(IdentityBenefitRelation::getTimeUpdate, now) .set(IdentityBenefitRelation::getOperator, idUser) .where(IdentityBenefitRelation::getBenefitCode).eq(code) .and(IdentityBenefitRelation::getIdIdentity).eq(update.getId()) .update(); } //找出共有的benefitCode并更新config List<String> bothExistBenefitCodes = voBenefitCodes.stream() .filter(dbBenefitCodes::contains) .collect(Collectors.toList()); if (!bothExistBenefitCodes.isEmpty()) { for (String benefitCode : bothExistBenefitCodes) { BenefitItem result = update.getMatchBenefits().stream() .filter(item -> benefitCode.equals(item.getCode())) .findFirst() .orElseThrow(() -> new NoSuchElementException("没找到code:" + benefitCode)); JSONObject benefitConfig = result.getConfig(); UpdateChain.of(IdentityBenefitRelation.class) .set(IdentityBenefitRelation::getTimeUpdate, now) .set(IdentityBenefitRelation::getOperator, idUser) .set(IdentityBenefitRelation::getBenefitConfig,benefitConfig) .where(IdentityBenefitRelation::getBenefitCode).eq(benefitCode) .and(IdentityBenefitRelation::getIdIdentity).eq(update.getId()) .update(); } } //找出入参有但数据库没有的 benefitCode List<String> voOnlyBenefitCodes = voBenefitCodes.stream() .filter(code -> !dbBenefitCodes.contains(code)) .collect(Collectors.toList()); if (!voOnlyBenefitCodes.isEmpty()) { List<IdentityBenefitRelation> relations = new ArrayList<>(); //插入身份权益表 for (String benefitCode : voOnlyBenefitCodes) { //找到code对应的config BenefitItem result = update.getMatchBenefits().stream() .filter(item -> benefitCode.equals(item.getCode())) .findFirst() .orElseThrow(() -> new NoSuchElementException("没找到code:" + benefitCode)); if (result == null) continue; JSONObject config = result.getConfig(); IdentityBenefitRelation relation = new IdentityBenefitRelation(); relation.setOperator(idUser); relation.setIdIdentity(update.getId()); relation.setBenefitCode(benefitCode); relation.setBenefitConfig(JSONObject.toJSONString(config)); relation.setTimeCreate(now); relation.setTimeUpdate(now); relations.add(relation); } identityBenefitRelationDao.insertBatch(relations); } return true; } /** * 添加身份 * @param add * @return */ @Override @Transactional public Boolean addIdentity(ArguAddIdentity add, Long idUser) { LocalDateTime now = LocalDateTime.now(); Identity identity = new Identity(); String uuid = UUID.randomUUID().toString().replace("-", ""); identity.setCode(uuid); identity.setName(add.getName()); identity.setDescription(add.getDescription()); identity.setOperator(idUser); identity.setTimeCreate(now); identity.setTimeUpdate(now); identity.setMarkType(add.getMarkType()); identityAdminDao.insert(identity); Long id = identity.getId(); List<IdentityBenefitRelation> relations = new ArrayList<>(); //插入身份权益表 for (BenefitItem benefitItem : add.getMatchBenefits()) { String benefitCode = benefitItem.getCode(); JSONObject config = benefitItem.getConfig(); IdentityBenefitRelation relation = new IdentityBenefitRelation(); relation.setOperator(idUser); relation.setIdIdentity(id); relation.setBenefitCode(benefitCode); relation.setBenefitConfig(JSONObject.toJSONString(config)); relation.setTimeCreate(now); relation.setTimeUpdate(now); relations.add(relation); } identityBenefitRelationDao.insertBatch(relations); return true; } /** * 更改权益 * @param update * @return */ @Override public Boolean updateBenefit(ArguUpdateBenefit update, Long idUser) { UpdateChain.of(Benefit.class) .set(Benefit::getName,update.getName()) .set(Benefit::getOperator,idUser) .set(Benefit::getDescription,update.getDescription()) .set(Benefit::getTimeUpdate, LocalDateTime.now()) .set(Benefit::getPic, JSONObject.toJSONString(update.getPic())) .set(Benefit::getConfig,JSONObject.toJSONString(update.getConfig())) .where(Benefit::getCode).eq(update.getCode()) .update(); return true; } private BenefitInfo convertToBenefitInfo(BenefitInfoSub benefitInfoSub) { BenefitInfo benefitInfo = new BenefitInfo(); benefitInfo.setId(benefitInfoSub.getId()); benefitInfo.setCode(benefitInfoSub.getCode()); benefitInfo.setName(benefitInfoSub.getName()); benefitInfo.setDescription(benefitInfoSub.getDescription()); return benefitInfo; } private List<IdentityInfo> convertToIdentityInfo(List<IdentityInfoSub> records) { List<IdentityInfo> identityInfoList = new ArrayList<>(); for (IdentityInfoSub sub : records) { IdentityInfo info = new IdentityInfo(); info.setId(sub.getId()); info.setName(sub.getName()); info.setDescription(sub.getDescription()); info.setBenefit(sub.getBenefit()); identityInfoList.add(info); } return identityInfoList; } } 把这里能抽出来的使用泛型写成公共的
最新发布
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值