我想创建一个使用互联网,我想创建一个函数来检查连接是否可用,如果不是,去,有一个重试按钮,并解释活动的应用程序。
附件是我code,到目前为止,但我得到的错误语法错误,将}来完成MethodBody。
现在我已经将这些在试图得到它的工作,但至今没有运气...任何帮助将是AP preciated。
公共类TheEvoStikLeagueActivity延伸活动{
私人最终诠释SPLASH_DISPLAY_LENGHT = 3000;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.main);
私人布尔checkInternetConnection(){
ConnectivityManager conMgr =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
//我们是否连接到网络
如果(conMgr.getActiveNetworkInfo()!=空
&功放;&安培; conMgr.getActiveNetworkInfo()。isAvailable()
&功放;&安培; conMgr.getActiveNetworkInfo()。isConnected()){
返回true;
/ *新的处理程序,开始菜单 - 活动
*有些秒钟后关闭这个启动画面。* /
新的处理程序()。postDelayed(新的Runnable(){
公共无效的run(){
/ *创建一个Intent,将开始菜单 - 活动。 * /
意图mainIntent =新的意图(TheEvoStikLeagueActivity.this,IntroActivity.class);
TheEvoStikLeagueActivity.this.startActivity(mainIntent);
TheEvoStikLeagueActivity.this.finish();
}
},SPLASH_DISPLAY_LENGHT);
} 其他 {
返回false;
意图connectionIntent =新的意图(TheEvoStikLeagueActivity.this,HomeActivity.class);
TheEvoStikLeagueActivity.this.startActivity(connectionIntent);
TheEvoStikLeagueActivity.this.finish();
}
}
}
解决方案
此方法检查是否移动终端连接到互联网,并返回true,如果连接的:
私人布尔isNetworkConnected(){
ConnectivityManager厘米=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
返回cm.getActiveNetworkInfo()!= NULL;
}
在清单中,
<使用-权限的Android:名称=android.permission.ACCESS_WIFI_STATE/>
<使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/>
编辑: 这个方法实际上检查,如果装置被连接到互联网(有它连接到网络,但一个可能不互联网)。
公共布尔isInternetAvailable(){
尝试 {
InetAddress类IPADDR = InetAddress.getByName(google.com); //你可以用你的名字替换它
如果(ipAddr.equals()){
返回false;
} 其他 {
返回true;
}
}赶上(例外五){
返回false;
}
}
I want to create an app that uses the internet and I'm trying to create a function that checks if a connection is available and if it isn't, go to an activity that has a retry button and an explanation.
Attached is my code so far, but I'm getting the error Syntax error, insert "}" to complete MethodBody.
Now I have been placing these in trying to get it to work, but so far no luck... Any help would be appreciated.
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();
}
}
}
解决方案
this method checks whether mobile is connected to internet and returns true if connected:
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
in manifest,
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Edit: This method actually checks if device is connected to internet(There is a possibility it's connected to a network but not to internet).
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
if (ipAddr.equals("")) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
相关推荐
最新文章