问题
I want to "minimize" the application, leaving it in background doing exactly the same that when the ghostbutton mode is pressed when the user clicks a button (but don't finish it) How can I do that?
So far I'm able to create a Activity
. I've initialize my members and load the WebView
with "https://www.youtube.com". I'm also able to create a Service
that lets me minimize the Activity
, but what I want is to minimize the Activity
that I've my WebView
loaded. Problem When the Activity
paused, WebView
is also paused.
What I want now.
- A ghost mode -> basically minimizing this current activity with system overlay and showing notification for media controls that can play/resume and exit from the ghost mode.
- A
MainActivity
that is visible to me and only becomes the sevice and show meNotification
control after pressing ghostmode btn
What I've handled so far
Screen orientation, meaning Activity
doesn't recreate the WebView
. If I'm watching some video,it just pause the video on screen orientation.
What answers I want
- Can I make my current
Activity
to become a service? - If no, can I just create a
SplashActivity
that opens this makeMainActivity
and make thisService
on button click.
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>
MainActivity
@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);
}
These are my settings.
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);
}
Method that create my notification. This notification resumes the MainActivity
.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"classghostservice-> onstartCommand()");
foregroundNotification(1);
return START_NOT_STICKY;
}
回答1:
[SOLVED] After a week I'm able to answer this long awaited question of How to Run Youtube In background using Webview. Yes, webview requires UI to run we can't run it in background using service, even if you do find a you way, you will still get in issues.
The clever tricks are as following:
Yes, you need to add
android.permission.SYSTEM_ALERT_WINDOW
because you want to make a floating window that stays on top of other apps.Let's say you have asked for permission and ready to start
MainActivity
, at this point forget aboutsetContentView(R.layout.activity_main);
instead you need to add your layout inWindowManager
usingwindowManager.addView(yourLayout, yourWindowParams);
activity_main.xml
In my layout my parent layout was DrawerLayout
<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".....
So, I had to define my member variable as follows
private static DrawerLayout windowMain;
MainActivity
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
windowMain = (DrawerLayout) inflater.inflate(R.layout.activity_main, null);
Now the main part is by using this
windowMain
, you have to callfindViewById()
eg
windowMain.findViewById(R.id.btn);
windowMain.findViewById(R.id.webview);
You also need to define its
WindowManager.LayoutParams
. Initializing params.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;
Finally, you need to add windowMain and its params into windowManager by
windowManager.addView(windowMain, expandParams);
Everything is done to make a window that floats around and take whole of your screen, then do what you need to do like loading URL
in webview using wv.loadURL(url)
. This ensures that we have loaded our webview inside a window and we just need to update the params of our windowMain
so that it minimize it or even completely disappear by setting w:0
and h:0
instead of MATCH_PARENT
You can have a btn_ghost
that updatesthe window like:
windowManager.updateViewLayout(windowMain, ghostParams);
Lastly, when we've created this created we can call
moveTaskToBack(true);
this will stop out activity move it to task, becuase we don't need it. When finishing this activity that was created but stopped we must remove ourwindowMain
.windowManager.removeView(windowMain); finish()
来源:https://stackoverflow.com/questions/54236715/how-to-play-youtube-in-background-with-android-webview