targetSdkVersion 26在崩溃报告中给出异常 [英] targetSdkVersion 26 giving exception in crash reports

查看:54
本文介绍了targetSdkVersion 26在崩溃报告中给出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据在2018年11月之前将目标SDK版本更新为26的要求,我几个月前尝试更新gradle文件并发布了我的应用程序的新版本,在更新了目标sdk和编译sdk版本之后到26(从23).在一天之内,我开始在Firebase控制台上观察我的应用程序中的崩溃.原因是我在我的应用程序中使用了TTS服务,并且当定位到版本26时,由于致命的运行时异常,它似乎需要对TTS服务进行新的实现.因此,我将目标SDK版本还原为23,并将sdk版本还原为23.

As per the requirement to update targeted SDK version to 26 by November 2018, I tried to update my gradle file a few months ago and released a new version of my app, after updating both target sdk and compile sdk versions to 26 (from 23). Within a day, I started observing crashes in my app on my Firebase console. The reason was I was using TTS service in my app, and when targeting to version 26, it looks like it required a new implementation for the TTS service, because of the fatal run-time exception. So I reverted the target SDK version to 23 and compile sdk version to 23.

Caused by java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.a.b.c/services.TTSService }: app is in background

对于Android OS 8.0和8.1上的设备,此异常仅显示为 .

This exception was seen only for the devices on Android OS 8.0 and 8.1.

因此,一个月前,我购买了一个新设备,该设备位于Android 8.0(Motorola G6 Plus)上,希望能够始终如一地重现此错误.即使我的应用定位到版本26,即使我通过我的应用强行触发了TTS服务,该设备也从未发生过.因此,我对如何进行感到困惑.

So a month ago, I bought a new device which is on Android 8.0 (Motorola G6 Plus), hoping to consistently reproduce this error. But it never happened on this device, even though my app targeting version 26, even when I forcefully triggered the TTS service through my app. So, I am confused as to how to proceed.

此异常的解决方案在我当前用于实现和调用TTS服务的代码如下:

My current code for implementing and calling the TTS service is as follows:

MainActivity oncreate方法:

MainActivity oncreate method :

 if(!isMyServiceRunning(TTSService.class))//Start a service instance only if service is currently not running
        startService(new Intent(MainActivity.this, TTSService.class));

TTSService.java:

TTSService.java:

package services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;  
import android.os.Environment;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.google.firebase.crash.FirebaseCrash;

import constants.ConstantParams;

public class TTSService extends Service {

private static TextToSpeech voice =null;

public static TextToSpeech getVoice() {
    return voice;
}

@Nullable
@Override

public IBinder onBind(Intent intent) {
    // not supporting binding
    return null;
}

public TTSService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    try{
            voice = new TextToSpeech(TTSService.this, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(final int status) {


                }
            });
    }
    catch(Exception e){
        e.printStackTrace();
        FirebaseCrash.report(new Exception("Error in initializing TTS object"));
        FirebaseCrash.log("Error in initializing TTS object");
    }


    return Service.START_STICKY;
}

@Override
public void onDestroy() {
    clearTtsEngine();
    super.onDestroy();

}

public static void clearTtsEngine()
{
    if(voice!=null)
    {
        voice.stop();
        voice.shutdown();
        voice = null;
    }
}

}

是否必须按照上述解决方案重新实现此功能?还是我可以采用一种更简单的方法来定位版本26 SDK,但将编译SDK版本保留为23(这是我应用程序中的当前状态)?任何其他解决方案将不胜感激.

Do I have to re-implement this feature as per the above mentioned solution? Or can I resort to a simpler approach of targeting the version 26 SDK, but retaining compile SDK version at 23 (it's current state in my app)? Any other solutions would be appreciated.

推荐答案

在android 8(api 26)+上,您无法在后台运行服务.您可以通过在清单中添加权限服务前台并调用 startServiceForeground()来在前台运行服务.

On android 8(api 26)+ you unable to run services in background. Thing you can do it is to run service in foreground by adding permission service foreground in manifest and by calling startServiceForeground().

这篇关于targetSdkVersion 26在崩溃报告中给出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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