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

2020-12-30  本文已影响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>

上一篇下一篇

猜你喜欢

热点阅读