美文网首页
完美解决Android8.0之后StartService报错问题

完美解决Android8.0之后StartService报错问题

作者: 静默的小猫 | 来源:发表于2020-12-30 09:28 被阅读0次

为了缩短冷启动时间,采取异步初始化sdk

步骤:

1.Application onCreate() 

var intent = Intent(instance, InitSDKService::class.java)

intent.action ="com.initsdkservice.init"

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

    InitSDKService().enqueueWork(this,intent)

}else{

  startService(intent)

}

2.

class InitSDKService : JobIntentService{

    constructor() :super()

    /**

* Unique job ID for this service.

*/

    val JOB_ID =1000

    /**

* Convenience method for enqueuing work in to this service.

*/

    fun enqueueWork(context: Context, work: Intent) {

        enqueueWork(context, InitSDKService::class.java,JOB_ID, work)

}

    override fun onHandleWork(intent: Intent) {

        //数据库初始化

        DataManager.init(BaseApplication.instance)

}

}

3.AndroidManifest.xml

<service android:name=".InitSDKService"

    android:permission="android.permission.BIND_JOB_SERVICE">

    <intent-filter >

        <action android:name="com.initsdkservice.init"/>

    </intent-filter>

</service>

相关文章

网友评论

      本文标题:完美解决Android8.0之后StartService报错问题

      本文链接:https://www.haomeiwen.com/subject/aavcoktx.html