荫试图创建连接到网络服务器和retricve数据的应用程序,我希望做一些功能
当我点击我的应用程序,它首先检查网络连接是否已启用?如果启用了启动应用程序,否则打开网络连接settings..after它重定向到我的应用程序.... 当应用程序连接到Web服务器,连接特定的时间后超时如果连接不是成功。解决方案在我的申请,我也这样定义一个类:
公共类ConnectionDetector { 私人语境_context; 公共ConnectionDetector(上下文的背景下){ this._context =背景; } / ** *检查所有可能的互联网服务提供商 * ** / 公共布尔isConnectingToInternet(){ ConnectivityManager连接=(ConnectivityManager)_context.getSystemService(Context.CONNECTIVITY_SERVICE); 如果(连接!= NULL) { 的NetworkInfo []信息= connectivity.getAllNetworkInfo(); 如果(信息!= NULL) 的for(int i = 0; I< info.length;我++) 如果(资讯[I] .getState()== NetworkInfo.State.CONNECTED) { 返回true; } } 返回false; } }
和中,我需要检查连接状态的活动,我用这个:
//定义了这个全局变量 AlertDialogManager警报=新AlertDialogManager();
![android系统启动流程](https://p.xsw88.cn/allimgs/daicuo/20230907/4570.png)
在的onCreate()
:
ConnectionDetector CD =新ConnectionDetector(getApplicationContext()); //检查网络present 如果(!cd.isConnectingToInternet()){ //没有Internet连接present alert.showAlertDialog(MainActivity.this,网络连接错误 请连接的Internet连接,FALSE); //停止执行回code 返回; }
哦。差点忘了。如果您还需要重新将用户引导到设置面板,激活互联网上,你可以使用一个意图是这样的:
您可能会促使在AlertDialog用户,并让他们选择,如果他们想。如果是的话,运行这块code的。
意向意图=新意图(Settings.ACTION_WIFI_SETTINGS);startActivity(意向);
编辑:
我错过了明显的( Commonsware指出,一出在他的评论中的)。
您需要在许可权的权限添加到您的清单文件。
<使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>
Iam trying to create an application that connect to a webserver and retricve data, i want to do some functions
when i click my application, first it check whether the internet access is enabled? if it is enabled it start the application , else open the internet access settings..after that it redirect to my application.... when the application is connecting to web-server, connection is timed out after a specific time if the connection is not success.解决方案
In an application of mine, I have a class defined like this:
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
And in the Activity that I need to check the connectivity status, I use this:
// DEFINE THIS AS A GLOBAL VARIABLE
AlertDialogManager alert = new AlertDialogManager();
In the onCreate()
:
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
Oh. Almost forgot. If you also need to re-direct the user to the settings panel to activate Internet, you can use an Intent like this:
You could prompt the user in the AlertDialog and let them choose if they want to. If yes, run this piece of code.
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);
EDIT:
I missed the obvious (Commonsware pointed that one out in his comment).
You will need to add the ACCESS_NETWORK_STATE permission to your Manifest file.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
相关推荐
最新文章