第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

應(yīng)用程序關(guān)閉后,Android 服務(wù)不存在

應(yīng)用程序關(guān)閉后,Android 服務(wù)不存在

郎朗坤 2022-07-20 16:33:36
我想要一個后臺服務(wù),它會在應(yīng)用程序關(guān)閉后保持活動狀態(tài),并且在應(yīng)用程序啟動時我可以再次綁定。為了進(jìn)行測試,我將每次綁定到服務(wù)時計(jì)數(shù)器都會增加。所以理論上應(yīng)用程序應(yīng)該啟動,我將創(chuàng)建服務(wù),然后綁定到它 -> 計(jì)數(shù)器應(yīng)該向上移動。然后我關(guān)閉應(yīng)用程序并再次按下綁定按鈕,它應(yīng)該記錄一個“1”并再次向上移動計(jì)數(shù)器。但它沒有......每次我重新啟動應(yīng)用程序并綁定到它時它都會顯示一個 0......這是我當(dāng)前的測試 - 服務(wù) - 類:package com.programm.testapp;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class TestService extends Service {    /*     * Service Binder     */    private final IBinder iBinder = new TestService.LocalConnectionService();    public class LocalConnectionService extends Binder {        public TestService getService(){            return TestService.this;        }    }    /*     * Test var     * It should increase every time the app is started.     */    private int test;    @Override    public IBinder onBind(Intent intent) {        Log.d("mDEBUG", "Test: " + test);        test++;        return iBinder;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d("mDEBUG", "Service: Start Command");        return START_STICKY;    }}
查看完整描述

1 回答

?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個贊

如果您主動關(guān)閉應(yīng)用程序(通過從 Android 活動列表中關(guān)閉它),Android 很可能會終止您的服務(wù)。您可以在您的應(yīng)用程序 Logcat 中看到這一點(diǎn)。唯一真正的解決方法是前臺服務(wù)。


此外,onBind不會在每次綁定到服務(wù)時調(diào)用。從Android 文檔:


您可以同時將多個客戶端連接到一個服務(wù)。但是,系統(tǒng)緩存了IBinder服務(wù)通信通道。換句話說,只有在第一個客戶端綁定時,系統(tǒng)才會調(diào)用服務(wù)的 onBind() 方法來生成 IBinder。然后系統(tǒng)將相同的 IBinder 傳遞給綁定到相同服務(wù)的所有其他客戶端,而無需再次調(diào)用 onBind()。


其次,僅調(diào)用 onStartCommand 并不意味著重新創(chuàng)建服務(wù)。在服務(wù)生命周期內(nèi)可以多次調(diào)用。例如,每次調(diào)用 startService 時,都會執(zhí)行 onStartCommand,但不一定要重新創(chuàng)建服務(wù)。


此外,您似乎在關(guān)閉活動時沒有取消綁定服務(wù)。這會使您的活動泄漏 ServiceConnection 并且您的應(yīng)用程序崩潰。它將解釋為什么每次關(guān)閉并重新啟動應(yīng)用程序時都會看到重新創(chuàng)建的服務(wù)。


嘗試在您的活動的 onPause 方法中添加取消綁定:


@Override

void onPause() {

    super.onPause()

    unbindService(this.serviceConnectino)

}

工作配置可能如下所示。它使用專用服務(wù)函數(shù)而不是 onBind 來實(shí)現(xiàn)計(jì)數(shù)器的遞增:


我的綁定服務(wù).kt

package com.test


import android.app.Service

import android.content.Intent

import android.os.Binder

import android.os.IBinder

import android.util.Log


class MyBoundService : Service() {


    abstract class MyBinder: Binder() {

        abstract fun getService(): MyBoundService

    }


    val iBinder: MyBinder = object: MyBinder() {

        override fun getService(): MyBoundService {

            return this@MyBoundService

        }

    }


    private var counter = 0


    fun increment() {

        counter ++

        Log.i("MyBoundService", "Counter: ${counter}")

    }


    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        Log.i("MyBoundService", "startCommand");

        return super.onStartCommand(intent, flags, startId)

    }


    override fun onBind(p0: Intent?): IBinder? {

        counter++

        Log.i("MyBoundService", "Bound: ${counter}")

        return iBinder

    }


    override fun onUnbind(intent: Intent?): Boolean {

        Log.i("MyBoundService", "Unbound")

        return super.onUnbind(intent)

    }

}

MainActivity.kt

package com.test


import android.content.Intent

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import kotlinx.android.synthetic.main.activity_main.*

import android.content.ComponentName

import android.content.Context

import android.content.ServiceConnection

import android.os.IBinder

import android.util.Log

import com.test.MyBoundService


class MainActivity : AppCompatActivity() {



    private val serviceConnection: ServiceConnection = object: ServiceConnection {

        override fun onServiceDisconnected(p0: ComponentName?) {

            Log.i("MainActivity", "Service disconnected")

        }


        override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {

            Log.i("MainActivity", "Service connected")

            p1?.let {

                (p1 as MyBoundService.MyBinder).getService().increment()

            }

        }


    }


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        btn_create.setOnClickListener {

            val i = Intent(this@MainActivity, MyBoundService::class.java)

            startService(i)

        }


        btn_bind.setOnClickListener {

            val i = Intent(this@MainActivity, MyBoundService::class.java)

            bindService(i, serviceConnection, Context.BIND_AUTO_CREATE)

        }

    }


    override fun onPause() {

        super.onPause()

        unbindService(serviceConnection)

    }

}


查看完整回答
反對 回復(fù) 2022-07-20
  • 1 回答
  • 0 關(guān)注
  • 166 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號