获取本手机IP地址

简介:   MyIPAdressActivity.java   package com.gogler.MyIPAdress; import java.net.InetAddress; import java.

 

MyIPAdressActivity.java

 

package com.gogler.MyIPAdress;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MyIPAdressActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String MyIPAdress=GetHostIp();
        TextView tv=new TextView(this);
        tv.setText("MyIPAdress:"+MyIPAdress);
        setContentView(tv);       
       
    }
   
   public  String GetHostIp() {   
         try { 
             for (Enumeration<NetworkInterface> en = NetworkInterface 
                     .getNetworkInterfaces(); en.hasMoreElements();) { 
                 NetworkInterface intf = en.nextElement(); 
                 for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr 
                         .hasMoreElements();) { 
                     InetAddress inetAddress = ipAddr.nextElement(); 
                     if (!inetAddress.isLoopbackAddress()) {                      
                         return inetAddress.getHostAddress(); 
                      } 
                  } 
              } 
         } catch (SocketException ex) { 
          } catch (Exception e) { 
          } 
          return null; 
      } 

}

 

 

AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
    package="com.gogler.MyIPAdress"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />
   
<!-- 必须授权-INTERNET -->
   <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyIPAdressActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

相关文章
|
6天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
18006 12
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
|
17天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
29546 141
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
7天前
|
人工智能 JSON 监控
Claude Code 源码泄露:一份价值亿元的 AI 工程公开课
我以为顶级 AI 产品的护城河是模型。读完这 51.2 万行泄露的源码,我发现自己错了。
4612 20
|
6天前
|
人工智能 API 开发者
阿里云百炼 Coding Plan 售罄、Lite 停售、Pro 抢不到?最新解决方案
阿里云百炼Coding Plan Lite已停售,Pro版每日9:30限量抢购难度大。本文解析原因,并提供两大方案:①掌握技巧抢购Pro版;②直接使用百炼平台按量付费——新用户赠100万Tokens,支持Qwen3.5-Max等满血模型,灵活低成本。
1451 3
阿里云百炼 Coding Plan 售罄、Lite 停售、Pro 抢不到?最新解决方案