【问题标题】:Android Service not staying alive after app closes应用程序关闭后Android服务不存在
【发布时间】:2019-03-05 15:46:35
【问题描述】:

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

为了测试,我设置了每次绑定服务时计数器都会增加。


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

然后我关闭应用程序并再次按下绑定按钮,它应该记录一个“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;
    }
}

这是我当前的测试 - 活动:

package com.programm.testapp;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private TestService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button createButton = findViewById(R.id.button_create_service);
        createButton.setOnClickListener(this::createService);

        Button destroyButton = findViewById(R.id.button_destroy_service);
        destroyButton.setOnClickListener(this::destroyService);



        Button bindButton = findViewById(R.id.button_bind_service);
        bindButton.setOnClickListener(this::bindService);

        Button unbindButton = findViewById(R.id.button_unbind_service);
        unbindButton.setOnClickListener(this::unbindService);


    }

    private void createService(View v){
        Intent intent = new Intent(this.getBaseContext(), TestService.class);
        startService(intent);
    }

    private void destroyService(View v){
        Intent intent = new Intent(this.getBaseContext(), TestService.class);
        stopService(intent);
    }

    private void bindService(View v){
        Intent intent = new Intent(this.getBaseContext(), TestService.class);
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    }

    private void unbindService(View v){
        unbindService(serviceConnection);
    }


    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("mDEBUG", "Connection: on service connected");

            MainActivity.this.service = ((TestService.LocalConnectionService) service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("mDEBUG", "Connection: on service disconnected");
        }
    };
}

这是我的 AndroidManifest.xml - 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.programm.testapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".TestService"
            android:enabled="true"
            android:exported="false"></service>
    </application>
</manifest>

这是我之后的输出……

  1. 按下创建服务 - 按钮
  2. 按下绑定服务 - 按钮
  3. 按下解除绑定服务 - 按钮
  4. 关闭应用并重启
  5. 按下绑定服务 - 按钮

.../com.programm.testapp D/mDEBUG: Service: Start Command
.../com.programm.testapp D/mDEBUG: Test: 0
.../com.programm.testapp D/mDEBUG: Connection: on service connected
.../com.programm.testapp D/mDEBUG: Service: Start Command
.../com.programm.testapp D/mDEBUG: Test: 0
.../com.programm.testapp D/mDEBUG: Connection: on service connected

顺便说一句,当我关闭应用程序时调用第二个“服务:启动命令”......在我注意到一些新日志之后,还将调用服务的构造函数和“onCreate”方法 - 类用它。

这正常吗?


编辑:

当我只最小化应用程序而不是通过 Activity - Menu 关闭它时,行为正是我想要的!!!


编辑 2:

前台服务现在可以完成这项工作...... 我找不到任何其他解决方案

【问题讨论】:

    标签: java android service background-service foreground-service


    【解决方案1】:

    如果您主动关闭应用程序(通过从 Android 活动列表中关闭它),Android 很可能会终止您的服务。您可以在您的应用程序 Logcat 中看到这一点。唯一真正的解决方法是前台服务。

    此外,onBind不会在您每次绑定到服务时被调用。来自Android documentation

    您可以同时将多个客户端连接到一个服务。但是,系统缓存了IBinder服务通信通道。也就是说,只有在第一个客户端绑定时,系统才会调用服务的 onBind() 方法来生成 IBinder。然后系统将相同的 IBinder 传递给绑定到相同服务的所有其他客户端,而无需再次调用 onBind()。

    其次,仅调用 onStartCommand 并不意味着重新创建服务。在服务生命周期内可以多次调用。例如,每次调用 startService 时,都会执行 onStartCommand,但不一定要重新创建服务。

    此外,您在关闭活动时似乎没有取消绑定服务。这会使您的活动泄漏 ServiceConnection 并且您的应用程序崩溃。它将解释为什么每次关闭并重新启动应用程序时都会看到重新创建的服务。

    尝试在活动的 onPause 方法中添加取消绑定:

    @Override
    void onPause() {
        super.onPause()
        unbindService(this.serviceConnectino)
    }
    

    工作配置可能如下所示。它使用专用的服务函数而不是 onBind 来实现计数器的递增:

    MyBoundService.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)
        }
    }
    

    【讨论】:

    • 正如我在问题中定义的那样,我确实按下了取消绑定服务的“取消绑定服务 - 按钮”。即使在 onPause() 方法中调用 unbindService - 函数,它的行为仍然相同。我还在服务的“onUnbind”功能中添加了一个日志,它正在被调用......所以这应该不是问题......
    • 您是否阅读了我关于 onbind 的答案的第一部分,而您每次调用 bindService 时实际上都没有被调用?
    • 是的,这是正确的,我应该注意它,但这不是我目前的问题。问题是,每次我按下“绑定服务按钮”时,它似乎都在创建一个新的服务实例。 (我按照您的建议更改了代码,但仍然是相同的行为)
    • 只是调用了 onStartCommand 并不意味着重新创建了服务。如果你想弄清楚这一点,你应该在服务的 onCreate 方法或构造函数中注销一条消息。
    • 是的 onCreate 方法、onCommand 方法以及构造函数都被调用了。当我使用你的建议时,它仍然会每次输出相同的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多