我正在尝试在服务中创建纹理视图,但我似乎没有收到“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>