标签: android-networking

在后台运行WebView

我正在开发一个应用程序,它利用WebView登录网站,从中提取内容,然后在内容更新时通知用户.我已经获得了WebView来获取内容,但是我需要知道如何将WebView作为服务运行以使其在后台运行.

据我所知,必须从UI线程操纵WebViews,这使得事情变得更加艰难.

有关如何让应用程序通知用户的任何建议/解决方法,无论他们是否打开了应用程序?

android android-service android-networking android-webview

7
推荐指数
1
解决办法
9930
查看次数

如何在双卡安卓手机中获取连接到互联网的运营商名称?

我做了一个网络监控应用程序.在这里,我成功地实现了所有的事情.我有双卡通android手机.我知道如何获得运营商的名称.但我想知道哪个SIM连接到互联网?我使用过此代码,仅向用户显示设备是通过移动数据连接的.我想更具体地说,该设备目前正在使用哪个运营商的互联网.

public static String isInternetConnected (Context ctx) {
        ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
                .getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        // Check if wifi or mobile network is available or not. If any of them is
        // available or connected then it will return true, otherwise false;
        if (wifi != null) {
            if (wifi.isConnected()) {
                return "wifi";
            }
        }
        if (mobile != null) {
            if (mobile.isConnected()) {
                return "mobile";
            }
        }
        return "none";
    }
Run Code Online (Sandbox Code Playgroud)

android android-networking

7
推荐指数
1
解决办法
1306
查看次数

Android:应设置什么音频模式以在设备之间发送接收语音

我正在尝试在两个Android设备平板电脑和移动设备(通过Java套接字)之间传输语音/音频(双向).

平板电脑可以清晰播放收到的音频(语音),但手机播放的音频是噪音.
然后我在平板电脑上的代码中设置此音频模式:
audioManager.setMode(AudioManager.MODE_IN_CALL);

这导致移动接收清晰的语音.但平板电脑变得沉默,它不会播放收到的音频(或者说它听不到).

我不确定我应该在这里使用哪种AudioManager模式组合(如果有的话)?

android android-networking android-audiomanager android-audiorecord

7
推荐指数
1
解决办法
1048
查看次数

如何在翻新GET请求中发送对象参数?

我有一个像这样的后端服务器

"api/videos?search_object={"cat_id" :2, "channel_id" : 3, etc}
Run Code Online (Sandbox Code Playgroud)

您可以轻松地将搜索对象作为输入,它会基于该对象过滤列表。现在,我想将此服务与Retrofit一起使用,就像这样

@GET("videos")
Call<VideoListResponse> listVideos(@Query("search_object") VideoSearchObject videoSearchObject);
Run Code Online (Sandbox Code Playgroud)

但是上面的代码不起作用,我可以先将VideoSearchModel转换为JSON字符串,然后将其传递给它进行改造

@GET("videos")
Call<VideoListResponse> listVideos(@Query("search_object") String jsonString);
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更好更好的方法?任何建议将不胜感激。

android gson android-networking retrofit retrofit2

7
推荐指数
2
解决办法
4778
查看次数

使用 OkHttp 时是否可以限制带宽?

使用 OkHttp 来限制带宽时是否可能?(可能使用网络拦截器)。

android android-networking okhttp okhttp3

7
推荐指数
1
解决办法
497
查看次数

javax.net.ssl.SSLHandshakeException:java.lang.IllegalArgumentException:toASCII的输入无效:ip_nbae7bac35.kodrive.xyz

这个例外有解决方法吗?在github上看这个问题听起来它可能是一个android bug,有关主机名有下划线字符的东西.

这是堆栈跟踪:

javax.net.ssl.SSLHandshakeException: java.lang.IllegalArgumentException: Invalid input to toASCII: ip_nbae7bac35.kodrive.xyz
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:219)
        at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:318)
        at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:282)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:167)
        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
        at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
        at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall.execute(RealCall.java:77)
Run Code Online (Sandbox Code Playgroud)

android android-networking okhttp okhttp3

7
推荐指数
1
解决办法
900
查看次数

requestCellInfoUpdate() 的正确用法是什么?

利用onSignalStrengthsChangedgetAllCellInfo()和相关方法,我的应用程序监视细胞信号数据并实时显示结果。我的代码在针对 API 28 及更低版本时工作得非常好,可以在数据发生变化时自动刷新数据。以 API 29 为目标会导致部分Android 10 设备无法更新数据,但并非全部。

我发现TelephonyManager.requestCellInfoUpdate()已添加到 API 29,可能(?)需要它来解决此问题。但是,除了Android Reference 上的简洁定义之外,我无法找到有关此方法的任何信息。是否需要使用该方法来刷新cell信息?是否有任何代码示例或进一步的解释?

如果该方法不相关,API 29 中是否存在可能导致此行为的其他更改?ACCESS_FINE_LOCATION已确认获得批准,这似乎是唯一的其他相关 API 更改。

android android-networking android-10.0

7
推荐指数
1
解决办法
3174
查看次数

在Android中手动启动3G连接并保持开启状态

在启用WiFi的同时,如何在Android中启动3G数据连接?我试过了

IConnectivityManager.setMobileDataEnabled(enabled); // via reflection
Run Code Online (Sandbox Code Playgroud)

它可以在模拟器中运行,但在我的真实手机(Droid 2)中,它会短暂打开然后再次关闭.

从shell(adb shell),ip link提供3G连接的详细信息:

15: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 3 link/ppp

但是,它仅在WiFi关闭时可用.当WiFi打开并且我尝试手动打开它时,它会抱怨ppp0设备不存在.

bash-3.2# ip link set ppp0 up
ip link set ppp0 up
Cannot find device "ppp0"
Run Code Online (Sandbox Code Playgroud)

当我尝试列出设备时,我甚至找不到它

bash-3.2# ls /dev/ppp*
ls /dev/ppp*
/dev/ppp
Run Code Online (Sandbox Code Playgroud)

linux networking android android-networking

6
推荐指数
1
解决办法
3277
查看次数

Android DNS需要热身吗?

从旧的帖子如Android java.net.UnknownHostException:Host未解析(策略问题),它建议使用以下代码:

  try {
      InetAddress i = InetAddress.getByName(URLName);
    } catch (UnknownHostException e1) {
      e1.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

那么这意味着当DNS条目没有被缓存在设备中时,例如在启动之后,它将java.net.UnknownHostException: Host第一次返回,即使对于有效的DNS也是如此?

dns android android-networking

6
推荐指数
1
解决办法
1510
查看次数

使用volley库在android中进行Http身份验证

如何使用Volley库为API 进行Http身份验证

我尝试了以下代码....它抛出运行时异常和空指针异常.请提供建议

String url = "Site url";
String host = "hostName";
int port = 80;
String userName = "username";
String password = "Password";
DefaultHttpClient client = new DefaultHttpClient();
AuthScope authscope = new AuthScope(host, port);
Credentials credentials = new UsernamePasswordCredentials(userName, password);
client.getCredentialsProvider().setCredentials(authscope, credentials);
HttpClientStack stack = new HttpClientStack(client);
RequestQueue queue =  Volley.newRequestQueue(VolleyActivity.this, stack);
Run Code Online (Sandbox Code Playgroud)

android http-authentication android-networking android-volley

6
推荐指数
2
解决办法
8618
查看次数