我有以下代码用于检查我的应用程序上的互联网连接wifi/EDGE/GPRS/3G.
代码是
public static boolean checkConn(Context ctx) {
ConnectivityManager conMgr = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
return true;
} else if (conMgr.getNetworkInfo(0).getState()==NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED){
return false;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我打电话给它如下:
if (CheckInternet.checkConn(introPage.this) == true) {
Intent toMainPage = new Intent(introPage.this, mainPage.class);
System.gc();
startActivity(toMainPage);
} else if (CheckInternet.checkConn(getApplicationContext()) == false) {
Toast.makeText(getApplicationContext(),
"Sorry, No internet connectivity found", Toast.LENGTH_SHORT)
.show();
}
Run Code Online (Sandbox Code Playgroud)
但我有一个问题,那就是如果我连接到wifi,我打开应用程序,它工作正常,但如果我关闭应用程序并关闭wifi并重新打开应用程序,它不会通过错误"没有连接",我需要关闭我的设备,然后打开它,同样的情况是如果wifi关闭,我打开应用程序,它会抛出"无连接"的错误,如果我打开它,仍然它抛出"无连接"的相同错误,除非我关闭并打开设备.
我想以编程方式检查Android手机/模拟器中是否存在互联网连接.因此,一旦我确定存在互联网连接,那么我将打电话给互联网.
所以它就像"嘿模拟器!如果你有互联网连接,那么请打开这个页面,否则doSomeThingElse();"
java android internet-connection android-manifest android-emulator
我想使用提供方法activeNetworkInfo.type的连接管理器来检查Android中的网络类型。此方法在API级别28中已弃用。因此在API 28中检查网络类型的解决方案是什么。我的代码是:
/**
* Check Wi Fi connectivity
*/
fun isWiFiConnected(context: Context): Boolean {
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return connManager.activeNetworkInfo.type == ConnectivityManager.TYPE_WIFI
}
Run Code Online (Sandbox Code Playgroud)
我的摇篮就像:
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
}
Run Code Online (Sandbox Code Playgroud) 嗨,我正在研究我已集成BigQuery的Android应用程序.我看到有时在BigQuery表中插入记录时会遇到很多异常.我们不是这方面的专长,而是开始学习这项新技术.如果你们可以帮助我,那将是很棒的.
java.net.UnknownHostException: Unable to resolve host "accounts.google.com": No address associated with hostname
at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
at com.google.a.a.c.a.c.a(NetHttpRequest.java:77)
at com.google.a.a.c.r.p(HttpRequest.java:972)
at com.google.a.a.a.a.h.a(TokenRequest.java:307)
at com.google.a.a.b.a.a.b.f(GoogleCredential.java:384)
at com.google.a.a.a.a.c.h(Credential.java:489)
at com.google.a.a.a.a.c.a(Credential.java:217)
at com.google.a.a.c.r.p(HttpRequest.java:859)
at com.google.a.a.b.d.c.c(AbstractGoogleClientRequest.java:469)
at com.test.utils.c.c(CommonUtility.java:2730)
at com.test.services.AppInstallIntentService.onHandleIntent(AppInstallIntentService.java:71)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.getaddrinfo(Posix.java)
at …Run Code Online (Sandbox Code Playgroud) java android google-api-java-client google-api-client google-bigquery
如何使用Android服务进行ping回调?我需要在按钮单击时打开一个网页,但在后台,请ping另一个URL以进行统计信息收集.
我的设备与wifi连接,但如果连接wifi如何,但这些没有互联网连接
以下是我的代码,我试图检查是否没有互联网连接
public static boolean isConnectedWifi(Context context) {
NetworkInfo info=null;
if(context!=null){
info= IsNetConnectionAvailable.getNetworkInfo(context);
}
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
Run Code Online (Sandbox Code Playgroud)
当没有互联网访问时,它总是返回true
我试图回答这个(接受的答案)。我可以使用“ PING”方法,但是UI变黑了,因为它说它将阻塞UI线程。它看起来并不好并且令人不安,所以我尝试使用第二种方法“连接到Internet上的套接字”,但是我不知道如何在Kotlin中使用该类。
这是android studio将Java转换为kotlin的结果
package com.mockie.daikokuten.helpers
import android.os.AsyncTask.execute
import android.os.AsyncTask
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
internal class InternetCheck(private val mConsumer: Consumer) : AsyncTask<Void, Void, Boolean>() {
interface Consumer {
fun accept(internet: Boolean?)
}
init {
execute()
}
override fun doInBackground(vararg voids: Void): Boolean? {
try {
val sock = Socket()
sock.connect(InetSocketAddress("8.8.8.8", 53), 1500)
sock.close()
return true
} catch (e: IOException) {
return false
}
}
override fun onPostExecute(internet: Boolean?) {
mConsumer.accept(internet)
}
}
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何使用它。我这样尝试:
InternetCheck{ …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有以下代码...
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
return process.waitFor();
Run Code Online (Sandbox Code Playgroud)
...我发现它在某些设备(例如Motorola G)上工作(返回0)但在其他设备(例如Galaxy S3)上不起作用(返回2).我已经检查了Galaxy S3设备,它肯定有一个"/ system/bin/ping"文件,当然我已经确定它确实连接到了互联网.
任何人都有任何想法为什么命令可能无法在某些设备上工作......以及我可以做些什么来让它工作?
如果没有响应,应用程序会在这条线上崩溃。
chain.proceed(requestBuilder.build())
Run Code Online (Sandbox Code Playgroud)
这是我的 RetrofitClient 课程
class RetrofitClient {
private val apiService: ApiServiceInterface
init {
val builder = Retrofit.Builder()
builder.baseUrl(RequestParameters.BASE_URL)
builder.addConverterFactory(GsonConverterFactory.create())
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//added for adapter
builder.client(getClient())
val retrofit = builder.build()
apiService = retrofit.create(ApiServiceInterface::class.java)
}
companion object {
private var clientInstance: RetrofitClient? = null
lateinit var context: Context
fun getInstance(): RetrofitClient {
if (clientInstance == null) {
clientInstance = RetrofitClient()
}
return clientInstance as RetrofitClient
}
}
fun getApiInterface(): ApiServiceInterface {
return apiService
}
private fun getClient(): OkHttpClient {
val httpClient = OkHttpClient.Builder() …Run Code Online (Sandbox Code Playgroud) 我的应用程序首先从互联网加载数据(我正在使用webservice)我想在应用启动时检查互联网访问.
我该怎么做呢 ?
android ×10
kotlin ×3
connectivity ×2
java ×2
ping ×2
android-wifi ×1
connection ×1
deprecated ×1
httpclient ×1
retrofit2 ×1
rx-java ×1
service ×1
url ×1