我正在开发基于互联网的应用程序,所以我需要监控Internet连接.链接
我在创建mainActivity时使用了这段代码来检查我的互联网连接是否正常.
public boolean isOnline()
{
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE); return
cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
Run Code Online (Sandbox Code Playgroud)
但我需要通过应用程序完成此监控.我应该在哪里使用它?需要任何AsyncTask吗?
在我的代码中我使用的requestRouteToHost() 方法:
这种路由是否意味着将WIFI改为3G,反之亦然?
我的代码无效......
public static boolean isHostAvailable(Context context, String urlString) throws UnknownHostException, MalformedURLException {
boolean ret = false;
int networkType = ConnectivityManager.TYPE_WIFI;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm != null){
NetworkInfo nf = cm.getActiveNetworkInfo();
if(nf != null){
networkType = nf.getType();
}
URL url = new URL(urlString);
InetAddress iAddress = InetAddress.getByName(url.getHost());
ret = cm.requestRouteToHost(networkType, ipToInt(iAddress.getHostAddress()));
}
return ret;
}
public static int ipToInt(String addr) {
String[] addrArray = addr.split("\\.");
int num = 0;
for (int i=0;i<addrArray.length;i++) {
int power …Run Code Online (Sandbox Code Playgroud) 我正在使用下面的代码在android webview中加载url
webviewShowPost.loadUrl(URL);
Run Code Online (Sandbox Code Playgroud)
我想检查是否没有可用的数据连接,然后webview而不是显示空白视图,我可以显示没有连接的Toast.
谢谢
我正在使用HttpClient发布然后在Android中检索一些东西,效果很好,但是当我没有互联网连接它强制关闭时,他们是一种在制作HttpPost时捕获UnknownHostException的方法吗?我知道在提出请求之前我可以确保它连接到互联网,但如果手机没有服务怎么办?
我在这个过程中做得太迟了.我有很多屏幕/活动都需要连接到互联网,无论是wifi还是网络都无所谓.我可以检测到连接正常,但是我是否需要对每个活动执行此检查,或者是否有全局方式为我的应用程序执行此操作?
刚想到在添加大量代码之前我会问.
我有以下代码,我想要做的是以编程方式检查是否有互联网连接。
由于我收到NetworkOnMainThreadException,并被建议使用 AsyncTask。
我想执行网络操作hasActiveInternetConnection(Context context)并在连接到网络时返回 true ,否则返回 false 。
如何使用 AsyncTask 执行此操作?
public class NetworkUtil extends AsyncTask<String, Void, String>{
Context context;
public NetworkUtil(Context context){
this.context = context;
}
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
if (new CheckNetwork(context).isNetworkAvailable())
{
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
boolean url= (urlc.getResponseCode() == 200);
String str = String.valueOf(url);
return str;
} catch (IOException …Run Code Online (Sandbox Code Playgroud) 我做了一个表单应用程序,将数据发送到我的mysql服务器.一切都很好,但我有一个问题:如果我的应用程序没有互联网,我应该使用什么...如果我有互联网,用户输入数据并提交,但如果5或10分钟我没有互联网访问并且用户仍然发送数据.我是否丢失了这些数据,或者有没有办法保存数据?当互联网再次激活时,所有保存的数据都会传输到我的mysql服务器......?
我听说过JSON Parser,但我不确切知道它是如何工作的......
我创建了一个类来检查可用于将字符串上传到服务器的Internet.
我的应用程序在日志cat中没有显示任何错误或警告.所以,我不能发布任何日志.
但是当我连接或不连接到互联网时,它总是显示消息:You do not connect to the internet.在else子句中.
当我调试isInternetAvailable方法时,它转到第一行:
InetAddress ipAddr = InetAddress.getByName("google.com");
之后,它抛出异常,如:
cause = {NetworkOnMainThreadException} "android.os.NetworkOnMainThreadException"
并返回false.
下面的代码显示检查互联网,所有代码在一个类extends BroadcastReceiver:
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
if (ipAddr.equals("")) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
@Override
public void onReceive(final Context context, final Intent intent) {
String textToServer = "text example";
if(isInternetAvailable()){
sendToServer(context, textToServer.toString());
} else {
Toast.makeText(context, …Run Code Online (Sandbox Code Playgroud) 我正在查看 Google I/O Android App iosched链接,发现他们在 helper/util 类中主要使用静态方法。但是,我发现很多人不推荐在辅助类中使用静态方法。
假设我有 3 个活动正在执行一些工作,例如显示警报对话框或通知,那么我需要在所有 3 个活动中添加相同的代码。如果我正在编写来自 10 个不同活动的文件怎么办。使用带有静态方法的辅助类不是比一遍又一遍地编写相同代码更好的方法吗?如果不是,那么最好的方法是什么。
public class NotificationHelper {
/**
* create notification
* @param context activity context
* @param title notification title
* @param contentText notification text
* @param mNotificationId notification id
*/
public static void setUpNotification(Context context, String title, String contentText, int mNotificationId) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context).setLargeIcon((BitmapFactory.decodeResource(context.getResources(),R.drawable.launcher)))
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle(title)
.setContentText(contentText).setPriority(NotificationCompat.PRIORITY_MAX);
Intent resultIntent = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT); …Run Code Online (Sandbox Code Playgroud) 我必须使用wifi(如果可用)或gprs(如果wifi不可用)将我的应用程序与服务器连接.这是我检查连接可用性的代码
public static final boolean isConnectionAvailable(Activity a)
{
ConnectivityManager cm = (ConnectivityManager)a.getSystemService(Context.CONNECTIVITY_SERVICE);
State mobile = cm.getNetworkInfo(0).getState();
State wifi = cm.getNetworkInfo(1).getState();
if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
{
return true;
}
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?谁能建议我一个更好的方法?
是否可以通过尝试连接到网址来检查手机是否可以访问互联网?如果它成功连接到网站,应用程序将正常运行,如果连接失败,将弹出alertdialog告诉用户问题.