• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to keep app running in background with screen off

I have an app that works great while the screen is on, but as soon as the screen is off it start slowing down until eventually in a couple of minutes is off. I notice that the mainActivity in the android studio tester stops if I turn the screen off, as soon as I turn it on the screen the main activity goes live again and the app works fine again. I have tried BATTERY_OPTIMIZATIONS, DATA_RESTRICTIONS, but it doesn't seems to work. can anybody help me on how to keep the mainActivity from stopping while the screen is off?

I have read about foreground service and tried too but I dont seem to find any service that works with web view

thanks. Attach is a pic of the main activity stooping in the android studio test environment, the green part is the app with the screen on

this is the code that I am using

public class MainActivity extends AppCompatActivity {
private WebView webView;

@override


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = findViewById(R.id.wb);




WebView webview = new WebView (this);
setContentView(webview);
webview.loadUrl("http://www.apple.com");
webview.getSettings().setJavaScriptEnabled(true);


}

public void startService(View v){
String input = webView.getUrl().toString();

Intent serviceIntent = new Intent(this, RunService.class);
serviceIntent.putExtra("inputExtra", input);

startService(serviceIntent);
}
}
 

Attachments

  • mainactivity.PNG
    mainactivity.PNG
    11.2 KB · Views: 279
Welcome to AF. I moved this to the dev area for better exposure. :)

You should put your syntax in [code][/code] tags to make it more readable.
Example:

[code]
Line 1
Line 2
Line 3
Line 4
[/code]

creates
Code:
Line 1
Line 2
Line 3
Line 4
 
Back
Top Bottom