应用程序关闭后,Android 服务不存在

我想要一个后台服务,它会在应用程序关闭后保持活动状态,并且在应用程序启动时我可以再次绑定。


为了进行测试,我将每次绑定到服务时计数器都会增加。


所以理论上应用程序应该启动,我将创建服务,然后绑定到它 -> 计数器应该向上移动。


然后我关闭应用程序并再次按下绑定按钮,它应该记录一个“1”并再次向上移动计数器。


但它没有......每次我重新启动应用程序并绑定到它时它都会显示一个 0......


这是我当前的测试 - 服务 - 类:


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;

    }

}


郎朗坤
浏览 193回答 1
1回答

Qyouu

如果您主动关闭应用程序(通过从 Android 活动列表中关闭它),Android 很可能会终止您的服务。您可以在您的应用程序 Logcat 中看到这一点。唯一真正的解决方法是前台服务。此外,onBind不会在每次绑定到服务时调用。从Android 文档:您可以同时将多个客户端连接到一个服务。但是,系统缓存了IBinder服务通信通道。换句话说,只有在第一个客户端绑定时,系统才会调用服务的 onBind() 方法来生成 IBinder。然后系统将相同的 IBinder 传递给绑定到相同服务的所有其他客户端,而无需再次调用 onBind()。其次,仅调用 onStartCommand 并不意味着重新创建服务。在服务生命周期内可以多次调用。例如,每次调用 startService 时,都会执行 onStartCommand,但不一定要重新创建服务。此外,您似乎在关闭活动时没有取消绑定服务。这会使您的活动泄漏 ServiceConnection 并且您的应用程序崩溃。它将解释为什么每次关闭并重新启动应用程序时都会看到重新创建的服务。尝试在您的活动的 onPause 方法中添加取消绑定:@Overridevoid onPause() {    super.onPause()    unbindService(this.serviceConnectino)}工作配置可能如下所示。它使用专用服务函数而不是 onBind 来实现计数器的递增:我的绑定服务.ktpackage com.testimport android.app.Serviceimport android.content.Intentimport android.os.Binderimport android.os.IBinderimport android.util.Logclass 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.ktpackage com.testimport android.content.Intentimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport kotlinx.android.synthetic.main.activity_main.*import android.content.ComponentNameimport android.content.Contextimport android.content.ServiceConnectionimport android.os.IBinderimport android.util.Logimport com.test.MyBoundServiceclass 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)    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java