如果互联网连接是错误的,它应该等待连接
If internet connection is false, it should wait to connect
我有通过两个操作检查互联网连接的代码。如果 Internet 连接打开,应用程序将启动一个特定的 activity。如果 Internet 断开,应用程序会显示一条消息,要求用户连接到 Internet 以继续正常使用该应用程序。
所以,我想打开一个无线设置页面,然后在用户 enabling Wi-Fi, it should open a SplashScreen2 activity
时打开。这在启用 WI-FI 或互联网连接后非常 important.Just。
但是,我的代码没有等待用户在 if connection == false
时连接到 Internet 然后变为 true
,应用程序只是关闭它,因为我输入了 finish();
。我不希望我的应用程序完成它,我不知道如何做我想做的事。我需要协助。
有我的代码:
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen_layout);
ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
final boolean connected = networkinfo != null && networkinfo.isAvailable()
&& networkinfo.isConnected();
Log.v("Network state : ", connected + "");
Thread splashThread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (waited < 5000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
// do nothing
} finally {
Looper.prepare();
if (connected == false) {
Splash.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast toast = Toast.makeText(Splash.this, "You must connect to the Internet to continue", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
**finish();**
} else {
finish();
startActivity(new Intent(Splash.this,
SplashScreen2.class));
}
Looper.loop();
}
}
};
splashThread.start();
}
}
而不是使用 finish();在您的代码中,您可以打开设置页面,以便用户可以连接到互联网,并且当您打开设置页面时,不要像这样完成当前的 activity:
Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
通过这种方式,用户可以再次尝试进入下一个activity。
我有通过两个操作检查互联网连接的代码。如果 Internet 连接打开,应用程序将启动一个特定的 activity。如果 Internet 断开,应用程序会显示一条消息,要求用户连接到 Internet 以继续正常使用该应用程序。
所以,我想打开一个无线设置页面,然后在用户 enabling Wi-Fi, it should open a SplashScreen2 activity
时打开。这在启用 WI-FI 或互联网连接后非常 important.Just。
但是,我的代码没有等待用户在 if connection == false
时连接到 Internet 然后变为 true
,应用程序只是关闭它,因为我输入了 finish();
。我不希望我的应用程序完成它,我不知道如何做我想做的事。我需要协助。
有我的代码:
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen_layout);
ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
final boolean connected = networkinfo != null && networkinfo.isAvailable()
&& networkinfo.isConnected();
Log.v("Network state : ", connected + "");
Thread splashThread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (waited < 5000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
// do nothing
} finally {
Looper.prepare();
if (connected == false) {
Splash.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast toast = Toast.makeText(Splash.this, "You must connect to the Internet to continue", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
**finish();**
} else {
finish();
startActivity(new Intent(Splash.this,
SplashScreen2.class));
}
Looper.loop();
}
}
};
splashThread.start();
}
}
而不是使用 finish();在您的代码中,您可以打开设置页面,以便用户可以连接到互联网,并且当您打开设置页面时,不要像这样完成当前的 activity:
Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
通过这种方式,用户可以再次尝试进入下一个activity。