如何使用Android Webview在后台播放YouTube?
我想“最小化”应用程序,让它留在后台,就像当用户点击按钮(但不完成)时按下ghostbutton模式一样。我该怎么做 到目前为止,我能够创建一个如何使用Android Webview在后台播放YouTube?,android,webview,android-mediaplayer,android-notifications,foreground-service,Android,Webview,Android Mediaplayer,Android Notifications,Foreground Service,我想“最小化”应用程序,让它留在后台,就像当用户点击按钮(但不完成)时按下ghostbutton模式一样。我该怎么做 到目前为止,我能够创建一个活动。我已初始化我的成员并用“”加载WebView。我还可以创建一个服务,使活动最小化,但我想要的是最小化已加载网络视图的活动问题当活动暂停时,网络视图也暂停 我现在想要什么。 private void settingWebview() { myoutube.setWebViewClient(new Myyoutube());
活动
。我已初始化我的成员并用“”加载WebView
。我还可以创建一个服务
,使活动
最小化,但我想要的是最小化已加载网络视图的活动
问题当活动
暂停时,网络视图
也暂停
我现在想要什么。
private void settingWebview() {
myoutube.setWebViewClient(new Myyoutube());
myoutube.getSettings().setLoadsImagesAutomatically(true);
myoutube.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
myoutube.getSettings().setBuiltInZoomControls(false);
myoutube.getSettings().setLoadsImagesAutomatically(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
myoutube.getSettings().setMediaPlaybackRequiresUserGesture(true);
myoutube.getSettings().setJavaScriptEnabled(true);
myoutube.getSettings().setPluginState(WebSettings.PluginState.ON);
}
重影模式->基本上使用
系统覆盖和显示媒体控件的通知,这些控件可以
播放/继续并退出重影模式
对我可见且仅成为服务的main活动
并显示
按ghostmode btn后的通知
控制
到目前为止我处理的事情
屏幕方向,意味着活动
不会重新创建网络视图
。如果我正在看一些视频,它只是暂停屏幕上的视频方向
我想要什么答案
我可以将我当前的活动
变成一项服务吗
如果否,我是否可以创建一个SplashActivity
,打开此makemain活动
,并在单击按钮时使此服务
AndroidManifest.xml
<service android:enabled="true" android:name=".Services.GhostModeService" />
android:hardwareAccelerated="true"
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这些是我的设置。
private void settingWebview() {
myoutube.setWebViewClient(new Myyoutube());
myoutube.getSettings().setLoadsImagesAutomatically(true);
myoutube.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
myoutube.getSettings().setBuiltInZoomControls(false);
myoutube.getSettings().setLoadsImagesAutomatically(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
myoutube.getSettings().setMediaPlaybackRequiresUserGesture(true);
myoutube.getSettings().setJavaScriptEnabled(true);
myoutube.getSettings().setPluginState(WebSettings.PluginState.ON);
}
GhostmodeService
private WindowManager mWindowManager;
private View mGhostmode;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"classghostservice-> onCreate()");
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mGhostmode = inflater.inflate(R.layout.layout_ghostmode,null,false);
//setting the layout parameters
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
500,
500,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.x=0;
params.y=0;
params.gravity=Gravity.END | Gravity.BOTTOM;
//getting windows services and adding the floating view to it
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mGhostmode, params);
}
方法创建我的通知。此通知将恢复main活动
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"classghostservice-> onstartCommand()");
foregroundNotification(1);
return START_NOT_STICKY;
}
[解决]一周后,我能够回答这个期待已久的问题,即如何使用Webview在后台运行Youtube。是的,webview需要UI才能运行,我们无法使用服务在后台运行它,即使您找到了自己的方法,您仍然会遇到问题
聪明的技巧如下:
是的,您需要添加android.permission.SYSTEM\u ALERT\u WINDOW
,因为您希望在其他应用程序上创建一个浮动窗口
main活动
,此时请忘记setContentView(R.layout.activity\u main)
相反,您需要使用WindowManager.addView(yourLayout,yourlwindowparams)在WindowManager
中添加布局代码>
<service android:enabled="true" android:name=".Services.GhostModeService" />
android:hardwareAccelerated="true"
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在我的布局中,父布局是抽屉布局
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent".....
main活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG,"classmain-> onCreate");
initializeM();
settingWebview();
initializeNavigationTab();
myoutube.loadUrl(URL);
ghostModeServiceIntent=new Intent(MainActivity.this,GhostModeService.class);
}
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
windowMain = (DrawerLayout) inflater.inflate(R.layout.activity_main, null);
windowMain
,您必须调用
findViewById()
windowMain.findViewById(R.id.btn);
windowMain.findViewById(R.id.webview);
WindowManager.LayoutParams
。初始化参数
WindowManager.LayoutParams=expandParams
expandParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
expandParams.gravity = Gravity.START | Gravity.TOP;
expandParams.x = 0;
expandParams.y = 0;
windowManager.addView(windowMain, expandParams);
wv.loadURL(URL)
在webview中加载URL
。这确保了我们已经在一个窗口中加载了我们的webview,我们只需要更新windowMain
的参数,通过设置w:0
和h:0
,而不是匹配父项,使其最小化甚至完全消失
您可以有一个更新窗口的btn\u ghost
,如下所示:
windowManager.updateViewLayout(windowMain, ghostParams);
最后,当我们创建了这个时,我们可以调用moveTaskToBack(true)代码>这将停止活动并将其移动到任务,因为我们不需要它。完成此创建但已停止的活动时,我们必须删除windowMain
windowManager.removeView(windowMain);
finish()
请修复您的示例代码。“WindowManager.LayoutParams=expandParams”。不以分号结尾,不能是赋值