8

我有一个非常奇怪的问题。

Text To Speech我的 logcat 中不断出现这种崩溃,但奇怪的是我的应用程序与这件事无关。不只是这个活动,我的整个应用程序根本不使用它。

08-04 03:47:19.321: E/ActivityThread(24755): Activity com.lablabla.similarsearch.WebViewActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@443065c0 that was originally bound here
08-04 03:47:19.321: E/ActivityThread(24755): android.app.ServiceConnectionLeaked: Activity com.lablabla.similarsearch.WebViewActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@443065c0 that was originally bound here
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1452)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.ContextImpl.bindService(ContextImpl.java:1440)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.content.ContextWrapper.bindService(ContextWrapper.java:496)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech.java:685)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:655)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:608)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.webkit.AccessibilityInjector$TextToSpeechWrapper.<init>(AccessibilityInjector.java:682)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.webkit.AccessibilityInjector.addTtsApis(AccessibilityInjector.java:483)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.webkit.AccessibilityInjector.addAccessibilityApisIfNecessary(AccessibilityInjector.java:168)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.webkit.AccessibilityInjector.updateJavaScriptEnabled(AccessibilityInjector.java:418)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.webkit.WebViewClassic.updateJavaScriptEnabled(WebViewClassic.java:1682)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.webkit.WebSettingsClassic.setJavaScriptEnabled(WebSettingsClassic.java:1125)
08-04 03:47:19.321: E/ActivityThread(24755):    at com.lablabla.similarsearch.WebViewActivity.onCreate(WebViewActivity.java:24)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.Activity.performCreate(Activity.java:5133)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.os.Looper.loop(Looper.java:137)
08-04 03:47:19.321: E/ActivityThread(24755):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-04 03:47:19.321: E/ActivityThread(24755):    at java.lang.reflect.Method.invokeNative(Native Method)
08-04 03:47:19.321: E/ActivityThread(24755):    at java.lang.reflect.Method.invoke(Method.java:525)
08-04 03:47:19.321: E/ActivityThread(24755):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-04 03:47:19.321: E/ActivityThread(24755):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-04 03:47:19.321: E/ActivityThread(24755):    at dalvik.system.NativeStart.main(Native Method)

这是该活动的代码:(它所做的只是从 获取一个 urlIntent并将其加载到 a WebView

public class WebViewActivity extends Activity {

    public static final String EXTRA_URL = "extra_url";

    private WebView webView;
    private String baseUrl;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setUserAgentString(
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/20 Safari/537.31");
        baseUrl = getIntent().getStringExtra(EXTRA_URL);
        webView.loadUrl(baseUrl);
    }
}
4

2 回答 2

13

我的 logcat 中不断出现这种崩溃,但奇怪的是我的应用程序与 Text To Speech 无关。不只是这个活动,我的整个应用程序根本不使用它。

您的应用程序确实与它无关。

除了当你打电话时:

webView.getSettings().setJavaScriptEnabled(true);

方法 WebViewClassic.updateJavaScriptEnabled(boolean) 被执行:

void updateJavaScriptEnabled(boolean enabled) {
    if (isAccessibilityInjectionEnabled()) {

 ==>>>> // Call placed to AccessibilityInjector.updateJavaScriptEnabled(boolean)
        getAccessibilityInjector().updateJavaScriptEnabled(enabled);
    }
}

在这里,在您的设备的情况下isAccessibilityInjectionEnabled()返回。true它检索当前启用的无障碍服务列表,并提供/支持AccessibilityServiceInfo.FEEDBACK_SPOKEN。如果此列表非空,true则返回。

向上移动堆栈跟踪:

android.webkit.AccessibilityInjector.addTtsApis()

最终创建一个TextToSpeech对象。

问题:TextToSpeech.connectToEngine()使用Context调用bindService()。上下文是您的 Activity 的上下文(从 WebView 传递)。我相信 Logcat 在WebViewActivity启动时会显示这个(或类似的东西):

成功绑定 com.google.android.tts

假设:当您的 Activity 在屏幕旋转时被销毁并重新创建时,TTS 服务仍然被绑定,而上下文不再有效。这就是异常所指的。

我尝试了以下方法来检验我的假设:

我没有在 xml 中定义 WebView,而是在 onCreate() 中动态创建了它:

webView = new WebView(getApplicationContext());

请注意,我使用应用程序的上下文代替了活动的(this)。上下文在方向更改时保持有效,因此不会引发异常。

要确认一个(或多个)无障碍服务已启用,请将此代码添加到您的活动的 onCreate() 中:

AccessibilityManager am = (AccessibilityManager)
                                   getSystemService(ACCESSIBILITY_SERVICE);

List<AccessibilityServiceInfo> listOfServices = 
                                 am.getEnabledAccessibilityServiceList(
                                   AccessibilityServiceInfo.FEEDBACK_ALL_MASK);

for (AccessibilityServiceInfo asi : listOfServices) {
    Log.i("", "Accessibility Service >>>>>>>>: " + asi.getSettingsActivityName());
Log.i("", "Accessibility Service >>>>>>>>: " +
                                 asi.loadDescription(getPackageManager()));

}

注意:我必须打开TalkBack(在设置 > 辅助功能下)才能复制问题。另外,我不确定为什么在强制停止应用程序后不会引发此异常。

基于用户 Robert Nekic 的评论:背压行为:使用活动的上下文时抛出异常。应用程序的上下文仍然有效。

有用信息:链接

于 2013-09-14T04:29:51.243 回答
1

您可以添加此代码段以避免此服务连接泄漏异常:

class YourActivity extends Activity {
WebView yourWebView;

    @Override
    public void onDestroy() {

        if (yourWebView != null) {
            yourWebView.getSettings().setJavaScriptEnabled(false);
        }
}

yourWebView.getSettings().setJavaScriptEnabled(false); 将触发 AccessibilityInjector 的调用。updateJavaScriptEnabled () 其中:

private void removeTtsApis() {
    if (mTextToSpeech != null) {
        mTextToSpeech.stop();
        mTextToSpeech.shutdown();
        mTextToSpeech = null;
    }

    mWebView.removeJavascriptInterface(ALIAS_TTS_JS_INTERFACE);
}

mTextToSpeech.shutdown() 只是关键点。

于 2014-11-23T10:11:24.610 回答