Android O-后台服务限制未按预期运行 [英] Android O - Background service limitation not working as expected

查看:104
本文介绍了Android O-后台服务限制未按预期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚针对Android O背景限制测试了现有的android应用.

I was just testing my existing android application against Android O background limitations.

在测试中,我发现了一个奇怪的行为.因此,基本上在我的应用程序中,我正在使用后台服务,并且我将在第一个活动中启动它.

In testing, I found a strange behavior. So basically in my application, I am using background service and I am starting it in very first activity.

因此,现在的问题是活动和后台服务启动后,我将使用后退"按钮关闭我的活动.因此,按照服务概念,它会一直在后台运行.

So now the issue is once the activity and background service gets started, I am closing my activity using the back button. So as per the service concepts, it keeps running in the background.

大约1分钟后,将调用onDestroy()方法调用后台服务,但该服务仍保持运行状态.理想情况下,应根据文档将其杀死.

After approx 1 min onDestroy() method gets called of background service but still the service keeps running. Ideally as per the documentation it should be get killed.

所以,我不知道这是一个问题还是什么.

So, I don't know whether it is an issue or what.

作为参考,我创建了一个示例代码,该代码反映了如下所示的相同情况:

For reference, I have created a sample code which reflects the same scenario which is as below:

步骤

  • 启动应用程序
  • 单击启动后台服务"按钮.
  • 使用后退"按钮关闭应用程序.

HomeActivity.java

package com.icpl.otest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.icpl.otest.service.MyService;

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    public void onStartServiceClick(View view) {
        Intent serviceIntent = new Intent(this, MyService.class);
        startService(serviceIntent);
    }
}

MyService.java

package com.icpl.otest.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    private static final String TAG = "MyService";

    private Handler mHandler;

    public MyService() {
        Log.i(TAG, "MyService constructor called.");
        mHandler = new Handler();
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand with intent:- " + intent);
        startShowingVisibility();
        return START_NOT_STICKY;
    }

    private void startShowingVisibility() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "I am alive");
                startShowingVisibility();
            }
        }, 3000);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy()");
        super.onDestroy();
    }
}

推荐答案

大约1分钟后,调用了onDestroy()方法的后台服务,但该服务仍保持运行状态

After approx 1 min onDestroy() method gets called of background service but still the service keeps running

不,不是.您的服务可能已经开始的 可能会继续运行(对于您来说,是无限的Handler循环").但是,您的过程可能随时终止.

No, it does not. The work that your service may have started may keep going (in your case, your infinite Handler "loop"). However, your process may be terminated at any moment.

这篇关于Android O-后台服务限制未按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆