java - 应用程序关闭后,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;
}
}
这是我目前的测试 - 活动:
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>
这是我之后的输出...
- 按下创建服务 - 按钮
- 按下绑定服务 - 按钮
- 按下解除绑定服务 - 按钮
- 关闭应用程序并重新启动它
- 按下绑定服务 - 按钮
:
.../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:
前台服务现在可以完成这项工作......我找不到任何其他解决方案
解决方案
如果您主动关闭应用程序(通过从 Android 活动列表中关闭它),Android 很可能会终止您的服务。您可以在您的应用程序 Logcat 中看到这一点。唯一真正的解决方法是前台服务。
此外,onBind
不会在每次绑定到服务时调用。从Android 文档:
您可以同时将多个客户端连接到一个服务。但是,系统缓存了IBinder服务通信通道。换句话说,只有在第一个客户端绑定时,系统才会调用服务的 onBind() 方法来生成 IBinder。然后系统将相同的 IBinder 传递给绑定到相同服务的所有其他客户端,而无需再次调用 onBind()。
其次,仅调用 onStartCommand 并不意味着重新创建服务。在服务生命周期内可以多次调用。例如,每次调用 startService 时,都会执行 onStartCommand,但不一定要重新创建服务。
此外,您似乎在关闭活动时没有取消绑定服务。这会使您的活动泄漏 ServiceConnection 并且您的应用程序崩溃。它将解释为什么每次关闭并重新启动应用程序时都会看到重新创建的服务。
尝试在您的活动的 onPause 方法中添加取消绑定:
@Override
void onPause() {
super.onPause()
unbindService(this.serviceConnectino)
}
工作配置可能如下所示。它使用专用服务函数而不是 onBind 来实现计数器的递增:
我的绑定服务.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)
}
}
推荐阅读
- ios - 当用户填写一些文本字段时如何更新 UILabel 的文本
- javascript - 如何在点击时传输输入值
- c# - 在不与数据表帖子重叠的范围内查找最大的 TimeSpan
- jquery - 在一个函数中返回视图和 json 数据
- yii - Yii 上是否有任何无头 CMS?
- java - Java - 附加到循环中的文件或打印整个字符串
- javascript - 如何在laravel中显示弹出警报
- google-apps-script - Google 表格脚本在共享链接中不起作用
- macos - docker:来自守护程序的错误响应:安装被拒绝:
- amazon-web-services - 如何为 aws 日志组找到相应的资源?