简体   繁体   English

我如何知道我的Android应用程序是否在后台运行?

[英]How do I know whether or not my Android app is running in background?

There is a WebView in my Android App. 我的Android应用程序中有一个WebView。

I make a toast to show the loading progress. 我举杯祝酒,以显示加载进度。

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            super.onProgressChanged(view, progress);

            toast.setText("Loading..." + String.valueOf(progress) + "%");
            toast.show();
        }
    });

The problem is that when I press "home" button or start another Activity, it still keep updating and showing the toast. 问题是,当我按下“主页”按钮或启动另一个活动时,它仍然会不断更新并显示吐司。

Is there way to check if the current Activity is showing? 有没有办法检查当前活动是否显示?

For instance: 例如:

if(getActivity().isVisible())
   toast.show();

You could set yourself a boolean flag in onStop() which gets called as you are being removed from the screen. 您可以在onStop()中设置一个布尔标志,当您从屏幕上移除时,它会被调用。

private boolean amShowing = false;

protected void onStart(){
    super.onStart();
    amShowing = true;
}

protected void onStop() {
    super.onStop();
    amShowing = false;
}

when with your if statment you can use 何时使用你的if语句你可以使用

if(amShowing) {
   toast.show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么知道一个android app小部件是否在后台 - how can I know whether an android app widget is in background 如何知道我的应用程序是否在Bluestacks上运行? - How to know whether my App is running on Bluestacks? 我如何知道我的SyncAdapter(addPeriodicSync)是否已在Android中注册? - How do I know whether my SyncAdapter (addPeriodicSync) registered in Android or not? 我如何知道应用程序在android中后台运行多长时间? - How do i know how long the application is running in background in android? 我如何知道我的应用程序是否正在使用 Robolectric 运行? - How do I know if my app is running with Robolectric? 如何从在后台运行的我的服务中知道运行状况最好的应用程序的程序包名称(android) - How to know Package Name of a Top running app from my service running in background (android) 如何知道我的android应用正在接收器中运行? - How to know my android app is running in the receiver? 如何查找应用程序是在后台运行还是在前台运行或被杀死的android - How to find whether app is running in background or foreground or killed android 如何查找应用程序是在后台运行还是被杀死的android? - How to find whether app is running in background or killed android? Android——我怎么知道应用是否自动启动 - Android——How can i know whether the app is autostart or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM