我想创建一个使用互联网的应用程序,我正在try 创建一个功能来判断连接是否可用,如果不可用,请转到一个有重试按钮和说明的活动.

附件是到目前为止我的代码,但我得到了错误Syntax error, insert "}" to complete MethodBody.

现在我一直在把这些放在试图让它工作的地方,但到目前为止还没有运气……任何帮助都将不胜感激.

public class TheEvoStikLeagueActivity extends Activity {
    private final int SPLASH_DISPLAY_LENGHT = 3000;

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

        private boolean checkInternetConnection() {
            ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
            // ARE WE CONNECTED TO THE NET
            if (conMgr.getActiveNetworkInfo() != null
                    && conMgr.getActiveNetworkInfo().isAvailable()
                    && conMgr.getActiveNetworkInfo().isConnected()) {

                return true;

                /* New Handler to start the Menu-Activity
                 * and close this Splash-Screen after some seconds.*/
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        /* Create an Intent that will start the Menu-Activity. */
                        Intent mainIntent = new Intent(TheEvoStikLeagueActivity.this, IntroActivity.class);
                        TheEvoStikLeagueActivity.this.startActivity(mainIntent);
                        TheEvoStikLeagueActivity.this.finish();
                    }
                }, SPLASH_DISPLAY_LENGHT);
            } else {
                return false;

                Intent connectionIntent = new Intent(TheEvoStikLeagueActivity.this, HomeActivity.class);
                TheEvoStikLeagueActivity.this.startActivity(connectionIntent);
                TheEvoStikLeagueActivity.this.finish();
            }
        }
    }

推荐答案

此方法判断mobile是否已连接到internet,如果已连接,则返回true:

private boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}

在 list 中,

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Edit:

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com"); 
        //You can replace it with your name
            return !ipAddr.equals("");

        } catch (Exception e) {
            return false;
    }
}

Android相关问答推荐

使用Retrofit2的API调用:我如何能够一直进行API调用,以更新数据而无需重新打开应用程序

替换- prop -中的值(adb shell getprop)

推断的类型是片段,但应为上下文

格雷德的两个星号是什么意思?非路径

为什么柱子的高度不都一样?

如何修复Google Play市场中有关已删除广告库的错误消息?

如何共享没有';t是否存在?(仅信息)在Android?

Camera2 将图像从 ImageReader 传递到 MediaRecorder

SQLite Kotlin 问题 - 没有数据库

更改当前活动并返回后,Android webview 滚动不起作用

围绕动态大小的内容包装 Jetpack Compose Row?

我的观点在jetpack compose中相互重叠

Jetpack Compose 重组竞争条件

如何在 Dolphin 中启用 android studio new logcat | 2021.3.1 金丝雀 6?

如何使用 ConstraintLayout 链接两个文本

compose FontFamily 错误:必须初始化变量

单个用户可以在 Firebase 身份验证中将多个电话号码链接到他的帐户吗?

如何在 Android 上的 RecyclerViewAdapter 中使用 DiffUtils

在 Android Studio 中自动在多台设备上运行应用

用于 ldpi、mdpi、hdpi、xhdpi 显示器的 android 初始屏幕尺寸? - 例如:ldpi 为 1024X768 像素