标签: android-networking

NetworkInfo和NetworkInterface之间的变通方法映射

我正面临2011年在跟踪器问题中报告的问题,并希望开发一个合适的解决方法.我想向用户显示所有Android设备的网络接口,并按类型分类,以及它们当前是否处于活动状态.我至少要针对API 15.

据我所知,有两种方法可以获取网络接口信息:

  1. 通过内置的Java NetworkInterface类使用NetworkInterface.getNetworkInterfaces().这将返回NetworkInterface包含如接口名称信息的对象,与接口等.对于我的设备(LG G3)相关联的所有IP地址,我得到这样的接口lo,wlan0rmnet0.这也会返回我需要的IP地址,MTU,子网掩码和广播地址等信息.

  2. 通过Android API ConnectivityManager服务使用aConnManager.getAllNetworkInfo().这将返回约20 NetworkInfo我的设备对象,大多数是不使用的,但它们包括类型MOBILE,WIFI,BLUETOOTH,等等.这不包括任何网络参数,如IP地址,MTU等如上所述.

跟踪器中解释的问题是没有内置的方法来映射NetworkInfo到a NetworkInterface或反之亦然,我想避免使用接口名称映射到适当的,NetworkInfo如果可能的话.

既然我有预感是不可能的,那么大多数供应商的所有潜在网络接口名称中都有一个列表吗?rmnet[0-9]对于使用Qualcomm基带处理器和wlan[0-9]WiFi等设备来说似乎很常见.

在最坏的情况下,我想如果接口名称与任何常见模板不匹配,我可以将接口类型列为未知.提前致谢.

android android-networking

42
推荐指数
1
解决办法
1404
查看次数

Google Cloud Messaging - 在网络状态发生变化之前,有时无法收到消息

在开发一个与GCM集成的小项目时,我偶然发现了一个奇怪的问题.

有时当我开始观看日志以查看是否收到消息时,在我改变网络状态(IE最初在WiFi上,如果我关闭WiFi并转移到移动数据,消息到达)之前,消息似乎没有通过精细).在我改变网络状态后,消息开始完全正常,一旦我将网络状态改回到之前的状态(在这种情况下,WiFi),消息将继续被接收,这同样适用.

该项目本身包括启动启动的能力(启动时启动GCMBaseIntentService),这也完全正常,我确信应用程序/服务正在运行,因为我在发生此问题时手动启动应用程序(其中还检查是否正在运行的服务,如果它不运行它,并检查它是否已注册).

有没有其他人遇到过这个问题,或者有任何指示我如何解决这个问题?我没有看到的消息的时间之间的日志中有很大帮助什么都没有收到,当他们(改变网络状态后).我已经浏览了GCM文档,并且看不到由于超时(在设备本身上)或任何可能影响此问题的配置选项而未收到消息的任何提及.

感谢所有帮助 - 我可以提供源如果需要的话,虽然它很难从Android的SDK提供的演示程序偏离.

android android-networking google-cloud-messaging

39
推荐指数
2
解决办法
2万
查看次数

获取我的wifi ip地址Android

如何在wifi下连接时获取手机的IP地址?

我在这里找到了一个方法,但它返回了类似24.182.239.255的东西,即使我在wifi下,我期待像192.168.1.10这样的东西.

我喜欢这样的东西:

if (you are under wifi)
    String ip4 = getWifiIP()
else
    String ip4 = getIPAddress with the method linked before
Run Code Online (Sandbox Code Playgroud)

非常感谢!

ip android android-networking android-wifi

39
推荐指数
4
解决办法
8万
查看次数

android sdk中网络事件的意图动作

我需要接收网络连接的广播,如网络连接,断开连接等.我正在使用广播接收器.任何人都可以告诉我我需要捕获哪些针对网络事件的意图行动,现在根据我在互联网上的搜索,我使用的是android.net.ConnectivityManager.CONNECTIVITY_ACTION.

这是我的广播接收器类:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NetworkStateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


    if (intent.getAction().equals(
            android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {

        // do something..
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我还添加了访问网络状态的权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Run Code Online (Sandbox Code Playgroud)

这是我在清单文件中声明此类的方法

<receiver class=".NetworkStateReceiver" android:name=".NetworkStateReceiver">
    <intent-filter>
            <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

如果我错了或者有任何其他方式来捕获网络事件,请建议我正确的意图行动.

android intentfilter android-networking

38
推荐指数
2
解决办法
4万
查看次数

示例:使用AsyncTask的Android双向网络套接字

我在Android上找到的大多数网络套接字示例都是单向的.我需要一个双向数据流的解决方案.我最终了解到了AsyncTask.此示例显示如何从套接字获取数据并将数据发送回它.由于正在接收数据的套接字的阻塞性质,阻塞需要在UI线程以外的线程中运行.

为了举例,此代码连接到Web服务器.按"Start AsyncTask"按钮将打开套接字.套接字打开后,Web服务器将等待请求.按"发送消息"按钮将向服务器发送请求.服务器的任何响应都将显示在TextView中.对于http,一旦发送了所有数据,Web服务器将断开与客户端的连接.对于其他TCP数据流,连接将一直保持到一侧断开连接.

截图:

应用程序截图

AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.exampleasynctask"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

水库\布局\ main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start AsyncTask"></Button>
<Button android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Message"></Button>
<TextView android:id="@+id/textStatus" android:textSize="24sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Status Goes Here" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

SRC\com.exampleasynctask\MainActivity.java:

package com.exampleasynctask;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress; …
Run Code Online (Sandbox Code Playgroud)

android android-networking android-asynctask

37
推荐指数
2
解决办法
9万
查看次数

如何获得IPV4格式的IP_ADDRESS

我试图获取设备的IP地址,即使用WIFI或3G连接.我收到了IPV6格式的IP地址,这是不可理解的.我想要IPV4格式的IP地址.我做了google但dint找到了任何合适的解决方案.

这是我用来获取设备IP地址的代码

public String getLocalIpAddress() {
    try {
        try {
        for (Enumeration<NetworkInterface> en = NetworkInterface
                .getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) 
            {
                InetAddress inetAddress = enumIpAddr.nextElement();
                System.out.println("ip1--:" + inetAddress);
                System.out.println("ip2--:" + inetAddress.getHostAddress());
                if (!inetAddress.isLoopbackAddress()) {


                    String ip = inetAddress.getHostAddress().toString();
                    System.out.println("ip---::" + ip);
                    EditText tv = (EditText) findViewById(R.id.ipadd);
                    tv.setText(ip);
                    return inetAddress.getHostAddress().toString();

                }
            }
        }
    } catch (Exception ex) {
        Log.e("IP Address", ex.toString());
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

ip1--:/fe80::5054:ff:fe12:3456%eth0%2
ip2--:fe80::5054:ff:fe12:3456%eth0
Run Code Online (Sandbox Code Playgroud)

它应该显示如下:

192.168.1.1
Run Code Online (Sandbox Code Playgroud)

请帮帮我..

android ip-address android-networking android-4.0-ice-cream-sandwich

23
推荐指数
2
解决办法
3万
查看次数

使用Android Retrofit进行POST

我是Android编程的新手,也在使用Retrofit.我已就这个主题做了大量研究,但未能找到特定于我需求的解决方案.我正在使用我们的API并尝试发出POST请求.我使用以下非Retrofit代码成功实现了此目的:

    private class ProcessLogin extends AsyncTask<Void, String, JSONObject> {
    private ProgressDialog pDialog;
    String email,password;

    protected void onPreExecute() {
        super.onPreExecute();
        inputEmail = (EditText) findViewById(R.id.email);
        inputPassword = (EditText) findViewById(R.id.password);
        email = inputEmail.getText().toString();
        password = inputPassword.getText().toString();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setTitle("Contacting Servers");
        pDialog.setMessage("Logging in ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected JSONObject doInBackground(Void... params) {
        HttpURLConnection connection;
        OutputStreamWriter request = null;
        URL url = null;   
        String response = null;         
        String parameters = "username="+email+"&password="+password;  
        try
        {
            url = new URL("http://.../api/login");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true); …
Run Code Online (Sandbox Code Playgroud)

android android-networking android-asynctask androidhttpclient retrofit

23
推荐指数
2
解决办法
5万
查看次数

屏幕打开时,Wi-Fi或CPU是否可以进入睡眠状态?

Android上有WakeLocks和WifiLocks - 如果我的屏幕永远不会关闭,我是否需要这些?

我使用持久连接(例如WebSockets)几十分钟甚至几小时.我的屏幕是否总是足以防止设备丢失这些连接(如果我们假设连接仍然可用且服务器正常)?

为了保持屏幕,我使用标准方式:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

文档说的是什么:

为了避免耗尽电池,一个闲置的Android设备很快就会睡着了.

(https://developer.android.com/training/scheduling/wakelock.html)

通常,当用户暂时不使用设备时,Wi-Fi无线电可能会关闭.

(http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html)

这是否意味着......

当用户长时间不与设备交互时(即使屏幕仍然打开),Wi-Fi无线电或CPU可能会进入睡眠模式或关闭?

而且,更具体地说,IntentService当仍有Activity屏幕保持开启时,后台任务(例如)Wi-Fi或移动数据连接是否会进入休眠状态?

android android-networking android-lifecycle

21
推荐指数
1
解决办法
1000
查看次数

为什么我应该使用OkHttp而不是android httpClient和AsyncTask

在SpeakerDeck(https://speakerdeck.com/pareshmayani/lazy-android-developers-be-productive)的Paresh Mayani演讲中,他说最好使用OkHttpRetrofit代替AsyncTask使用DefaultHttpClient.

我的问题是为什么?
为什么他们更快?
那些也是基于默认android类的库吗?
OkHttp和Retrofit有什么区别?

谢谢!

android android-networking androidhttpclient retrofit okhttp

20
推荐指数
1
解决办法
1万
查看次数

通过编程方式实现以太网连接(Android)(Root设备)

我有一个关于以太网的小问题.

我的三个问题是:

  1. 我们可以通过编程方式开启/关闭Ethernet吗?

  2. 我们可以编程启用/禁用Ethernet吗?

  3. 我们能以编程方式连接Ethernet吗?

上述问题是通过以下方式完成的Wifi.喜欢

  1. 我们可以通过编程方式开启/关闭Wifi.

  2. 我们可以通过编程方式启用/禁用Wifi.

  3. 我们可以通过编程连接Wifi使用WifiManager.

android是否提供任何EthernetManager类似WifiManager处理Ethernet

或者,如果这似乎不可行,那么我原来的要求是:

我要澄清的第一件事是"设备已经生根".

我可以操作设置(默认)吗?就像我不希望Settings.apk除了WIFI和之外的任何其他选项Ethernet.它应该只显示WifiEthernet.而已.我可以从"设置"中禁用所有选项,还是可以从"设置"中删除所有其他选项?

android ethernet android-networking

19
推荐指数
3
解决办法
2万
查看次数