我正在尝试在所有活动之上制作Youtube视频,所以我使用了SYSTEM_ALERT_WINDOW,现在我很难尝试从服务启动YoutubeSupportFragment,这是我在FloatingWindow服务中的onCreate方法
public class FloatingWindow extends Service{
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(linearLayoutParams);
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(400,400, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
parameters.x = 0;
parameters.y = 0;
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
linearLayout.addView(stop);
windowManager.addView(linearLayout, parameters);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
private WindowManager.LayoutParams updatedParameters = parameters;
int x, y;
float touchedX, touchedY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = updatedParameters.x;
y = updatedParameters.y;
touchedX = event.getRawX();
touchedY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
updatedParameters.x = (int) (x + (event.getRawX() - touchedX));
updatedParameters.y = (int) (y + (event.getRawY() - touchedY));
windowManager.updateViewLayout(linearLayout, updatedParameters);
break;
default:
break;
}
return false;
}
});
}
这是我想要作为浮动窗口显示的XML文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/COLOR_BACKGROUND">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/youtube_fragment"
android:name="com.example.youtube.YoutubePlayerView"/>
</LinearLayout>
问题是,在onCreate上我尝试用XML文件膨胀linearLayout,我得到了片段的ClassNotFoundException,我认为它很明显,因为我没有实例化片段。
我也不能使用FragmentTransaction将XML文件添加到我的linearLayout,因为我无法从服务类中访问FragmentManager。
任何想法如何让youtube播放器作为浮动窗口播放?
答案 0 :(得分:0)
在您的服务中,启动一项新活动,其唯一目的是附加/显示此片段。 如果您不想要此片段/活动的多个实例,请将您的活动设置为清单文件中的SingleTop