首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

ContextCompat.startForegroundService(context,intent)和startforegroundservice(intent)有什么区别?

ContextCompat.startForegroundService(context, intent)和startForegroundService(intent)是Android中用于启动前台服务的两个方法。

区别如下:

  1. 参数不同:ContextCompat.startForegroundService(context, intent)方法需要传入一个Context对象作为第一个参数,而startForegroundService(intent)方法不需要传入Context对象。
  2. 兼容性不同:ContextCompat.startForegroundService(context, intent)方法是兼容性方法,用于在Android 8.0及以上版本中启动前台服务。而startForegroundService(intent)方法是在Android 5.0及以上版本中引入的原生方法,用于启动前台服务。
  3. 权限要求不同:ContextCompat.startForegroundService(context, intent)方法需要在AndroidManifest.xml文件中声明RECEIVE_BOOT_COMPLETED权限,而startForegroundService(intent)方法不需要。

推荐的腾讯云相关产品和产品介绍链接地址: 腾讯云移动推送(https://cloud.tencent.com/product/tpns) 腾讯云移动直播(https://cloud.tencent.com/product/mlvb) 腾讯云云服务器(https://cloud.tencent.com/product/cvm) 腾讯云云数据库 MySQL 版(https://cloud.tencent.com/product/cdb_mysql) 腾讯云云存储(https://cloud.tencent.com/product/cos) 腾讯云人工智能(https://cloud.tencent.com/product/ai) 腾讯云物联网(https://cloud.tencent.com/product/iotexplorer) 腾讯云区块链(https://cloud.tencent.com/product/baas)

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

Android 8.0 简单适配那些事儿

Android 8.0 以发布很长时间了,基于用户设备市场要求等迟迟没有适配升级;如今适配时遇到一些问题,整理记录一下!...不能直接用 startService 启动后台服务; 方案一: 由后台服务转为前台服务,根据版本判断,使用 startForegroundService(),但是应用必须在创建服务后的五秒内调用该服务的...startForeground,甚至会出现 ANR 应用崩溃; // 启动 Service if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService...(5000); scheduler.schedule(builder.build()); } else { context.startService(new Intent...(context, TestJobService.class)); } } 方案三: 为了简便的使用 JobService Service,和尚尝试了三方的 android-job

1.2K21

Android O 后台startService限制简析

Android O 推出出了Background Execution Limits,减少后台应用内存使用及耗电,一个很明显的应用就是不准后台应用通过startService启动服务,这里两个问题需要弄清楚...前后台应用切换时机与原理 一个应用可以一个或者多个进程,当任何一个进程变为被转换成前台可见进程的时候,APP都会被认作前台应用(对于startService应用而言),resumetopActivity...不过本篇只看startForegroundService: @Override public ComponentName startService(Intent service) { warnIfCallingFromSystemProcess...= null) { mAm.mAppErrors.appNotResponding(app, null, null, false, "Context.startForegroundService...EXCEPTION: main Process: com.snail.labaffinity, PID: 21513 android.app.RemoteServiceException: Context.startForegroundService

12.2K30

Android实现进程保活方案解析

众所周知,日活率是一款App的核心绩效指标,日活量不仅反应了应用的受欢迎程度,同时反应了产品的变现能力,进而直接影响盈利能力企业估值。...如果是负数,表示该进程为系统进程,肯定不会被杀掉, 如果是0,表示是前台进程,即当前用户正在操作的进程,除非万不得已,也不会被杀掉; 如果是1,表示是可见进程,通常表示一个前台服务,会在通知栏一个划不掉的通知...startForegroundService(i); } else { startService(i); } 这种保活方式,会在通知栏常驻一条通知。...context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {//屏幕被关闭...Intent it = new Intent(context, OnepxActivity.class); it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK

8.5K30
领券