Как запустить интентсервис в котлине - PullRequest
0 голосов
/ 04 июля 2019

Я новичок в разработке Kotlin.ниже приведена попытка создать интентсервис.я следовал за некоторыми учебниками в Интернете, но во время выполнения я получил нижеупомянутую ошибку в logcat.пожалуйста, дайте мне знать, как я могу исправить эту ошибку

MainActivity :

public val LOG_TAG : String = "MainActivity";
public val BackgroundIntentServiceAction = "android.intent.action.CUSTOME_ACTION_1"

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val btnStartService = findViewById<Button>(R.id.btnStartService)
    btnStartService.setOnClickListener{startService()}
}

private fun startService() {
    val intent = Intent(this@MainActivity, BackgrounIntentService::class.java).apply {
        intent.setAction(BackgroundIntentServiceAction)
    }
    startService(intent);
}

intentservice :

class BackgrounIntentService : IntentService("BackgroundIntentService") {

private val LOG_TAG = "BackgroundIntentService"


override fun onCreate() {
    super.onCreate()
    Log.d(LOG_TAG, "onCreate")
}

override fun onHandleIntent(intent: Intent?) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    Log.d(LOG_TAG, "onHandleIntent")

}

}

манифест

class BackgrounIntentService : IntentService("BackgroundIntentService") {

private val LOG_TAG = "BackgroundIntentService"


override fun onCreate() {
    super.onCreate()
    Log.d(LOG_TAG, "onCreate")
}

override fun onHandleIntent(intent: Intent?) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    Log.d(LOG_TAG, "onHandleIntent")

}

}

ошибка logcat

2019-07-04 18:55:07.521 7642-7642/com.example.kotlin_v1 D/BackgroundIntentService: onCreate

--------- beginning of crash
2019-07-04 18:55:07.528 7642-7675/com.example.kotlin_v1 E/AndroidRuntime: FATAL EXCEPTION: IntentService[BackgroundIntentService]
Process: com.example.kotlin_v1, PID: 7642
kotlin.NotImplementedError: An operation is not implemented: not implemented
    at com.example.kotlin_v1.BackgrounIntentService.onHandleIntent(BackgrounIntentService.kt:18)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:76)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)
2019-07-04 18:55:07.704 7642-7660/com.example.kotlin_v1 D/EGL_emulation: eglMakeCurrent: 0x99d05060: ver 3 1 (tinfo 0x99d03140)
2019-07-04 18:55:07.710 7642-7660/com.example.kotlin_v1 D/OpenGLRenderer: endAllActiveAnimators on 0x97858e80 (RippleDrawable) with handle 0x99d03ad0

1 Ответ

2 голосов
/ 04 июля 2019

Эта ошибка происходит от:

TODO("not implemented")

Предположительно, это пришло из мастера Android Studio * - 1004 *. Идея состоит в том, что вы заменяете весь код TODO чем-то другим.

...