【问题标题】:how to close the android application if there is no internet如果没有互联网,如何关闭android应用程序
【发布时间】:2015-01-26 10:20:54
【问题描述】:

我正在尝试创建一个基于 google 表单的 android 项目,如果应用程序无法连接到互联网,我希望应用程序关闭或不显示链接,这是代码,已尝试使用退出,但它没有工作,任何帮助都会很棒。

package tscolari.mobile_sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class AndroidMobileAppSampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

    WebSettings webSettings = mainWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mainWebView.setWebViewClient(new MyCustomWebViewClient());
    mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    mainWebView.loadUrl("the url of form");
}

private class MyCustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}


i want something like this

if(mainwebview==null)//not able to load page
{
 //close the application
}

【问题讨论】:

  • 您想要代码,还是想要排除故障? “它不起作用”不是问题陈述。
  • 您的退出和检查互联网的代码在哪里??
  • 我实际上希望代码检查互联网连接,如果不可用则退出应用程序

标签: android webview


【解决方案1】:

使用它来检查互联网连接: [注意:这不是我最初的代码。我是从一个教程中拿来的,但我不知道是哪一个]

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class CheckNetworkConnection {

    public static boolean isConnectionAvailable(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()
                    && netInfo.isConnectedOrConnecting()
                    && netInfo.isAvailable()) {
                return true;
            }
        }
        return false;
    }
}

基本上,只需执行if(!CheckNetworkConnection.isConnectionAvailable(myContext)) {/* Close App*/ }。要关闭 Activity,请使用 finish()。但是,要关闭整个应用程序,read here,因为它比我在此处的简单答案中更好地回答了所有问题。请注意,在没有用户输入的情况下突然关闭应用程序不是一个好主意。

编辑:正如 Xaver Kapeller 在评论中澄清的那样,finish() 是关闭 Activity 的好方法 - 这也会关闭整个应用程序(除非 Activity 以 startActivityForResult() 或类似名称启动)

【讨论】:

    【解决方案2】:

    我正在检查互联网连接

    public static boolean isInternetAvailable(Context cxt) {
        final ConnectivityManager conMgr = (ConnectivityManager) cxt
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo i = conMgr.getActiveNetworkInfo();
    
        // if there is no active internet connection
        if (i == null || !i.isConnected() || !i.isAvailable()) {
            return false;
        }
    
        return true;
    }
    

    您可以使用 finish() 退出应用程序,但您应该让用户以某种方式知道您关闭应用程序的原因。

    【讨论】:

    • 如果我没记错的话,finish() 只是关闭 Activity,而不是整个应用程序。这里提供了有关关闭整个应用程序的更多信息:Click meh
    • (对于其他人来说,最好不要关闭整个应用程序,例如提示以某种方式启用互联网或在关闭前至少打开一个小对话框)
    • 是的,这可能是真的。但无论如何,正如该用户可能解释的(tldr),您永远不应该关闭这样的应用程序。只是显示一个对话框,吐司或互联网不可用,离线无事可做,让用户自己关闭应用程序。
    • 正在尝试检查互联网,上面的代码没有帮助,对不起
    • @MDragon00 操作系统处理应用程序生命周期,你不能做任何其他事情,只能使用finish() 关闭Activity,Android 将在不再需要时关闭应用程序。如果您使用像System.exit(0) 这样的其他东西,那么您只会导致主要 问题。 从不在 Android 上使用 System.exit(0) 或类似的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    相关资源
    最近更新 更多