Android 在服务中创建纹理视图时,从不调用onSurfaceTextureAvailable

Android 在服务中创建纹理视图时,从不调用onSurfaceTextureAvailable,android,textureview,Android,Textureview,我正在尝试在服务中创建纹理视图,但似乎没有收到“onSurfaceTextureAvailable”的回拨。“TextureView或子类只能在启用硬件加速的情况下使用。”当我为应用程序提供android:hardwareAccelerated=“true”时,会在日志中出现,是否有明确向服务提供的权限 package com.example.textures; import android.app.Service; import android.content.Context; impo

我正在尝试在服务中创建纹理视图,但似乎没有收到“onSurfaceTextureAvailable”的回拨。“TextureView或子类只能在启用硬件加速的情况下使用。”当我为应用程序提供android:hardwareAccelerated=“true”时,会在日志中出现,是否有明确向服务提供的权限

package com.example.textures;



import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.ViewGroup;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FlyMaadi extends Service implements SurfaceTextureListener {


    private final String TAG = "FlyMaadi";
    private TextureView mTextureView;
    private WindowManager mWindowManager;
    private LayoutInflater minflater;
    private ViewGroup mrel_layout;
    private TextureView mTextureView1;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    public void createSurfaceTexture()
    {
        Log.d(TAG,"Create Surface Texture\n");
        mTextureView = new TextureView(this);

        Log.d(TAG,"Is hardware accelerated:: "+ mTextureView.isHardwareAccelerated());


        mTextureView.setSurfaceTextureListener(this);

        //mTextureView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

        minflater = (LayoutInflater)getSystemService
                (LAYOUT_INFLATER_SERVICE);



        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        FrameLayout mParentView = new FrameLayout(getApplicationContext());


        final WindowManager.LayoutParams params1 = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params1.width = 352;
        params1.height = 288;

        //mWindowManager.addView(mrel_layout, params1);

        mWindowManager.addView(mParentView, params1);


        if(mTextureView.isAvailable())
        {
            Log.d(TAG,"Texture view is already available");
        }
        else
        {
            Log.d(TAG,"Texture view is not available");
        }


        Log.d(TAG,"Adding surface texture:: ");


        mParentView.addView(mTextureView);






    }


    @Override 
    public void onCreate() {
        super.onCreate();

        Log.d(TAG,"onCreate");
        createSurfaceTexture();

    }


    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureAvailable");
    }


    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureDestroyed");
        return false;
    }


    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureSizeChanged");

    }


    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureUpdated");

    }

}
我的Androidmanifest xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textures"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"        
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="true" 
        >
        <activity
            android:name="com.example.textures.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service            
            android:name="com.example.textures.FlyMaadi"
            />


    </application>

</manifest>

对于
“TextureView或子类只能在启用硬件加速的情况下使用。”
,您可能需要替换此行:

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE


我的问题是我正在使用AndroidAnnotations,我必须在这个类中处理没有AndroidAnnotations的所有内容。

一旦视图在屏幕上可见,就会调用onSurfaceTextureAvailable。在那之前,它不会被称为。我发现您没有将textureview(及其父级)添加到活动窗口或其任何视图中。尝试向服务传递活动视图id或视图,并将textureview添加到其中。显示视图后,应调用调用的。(顺便说一句,不建议从服务处理视图)

您是否尝试扩展IntentService而不是常规服务?“可能”是因为服务在主线程上运行,所以您遇到了这个问题。除此之外,请检查WindowManager实例的属性。
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED