Android(安卓)易忘部分
16lz
2021-01-26
1. 添加 service
在 activity 后面, application 添加, service 的类名标示在属性 name 中, 路径为从 src 下的包名到类名, 若只有一个包, 可以直接用 (.类名) 标示
2. java 获取本机地址
先是反例
//get localhost ip address InetAddress addr; try { addr = InetAddress.getLocalHost(); String ip = addr.getHostAddress().toString(); System.out.println("Listening on "+ip+":"+listener.getPort()); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
再是正解
Enumeration<NetworkInterface> ip; try { ip = NetworkInterface.getNetworkInterfaces(); while(ip.hasMoreElements()) { NetworkInterface iface = ip.nextElement(); if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); String desiredip = addr.getHostAddress(); System.out.println(iface.getDisplayName() + " " + desiredip); } } } catch (SocketException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
直接查询 IPconfig 就好了, 动态的话, 是有些 tricky, 上面的正解也并不是很有效
更多相关文章
- RemoteViews用法一:widget简单用法
- MonkeyRunner进坑——errors importing other modules
- Android添加多语言
- android GUI反编译工具 APKDecoderV0.9
- 5.3 ListView的HeaderView
- android 设置程序安装位置
- Android(安卓)Studio(2.3.3)配置Kotlin笔记
- Android(安卓)OpenCV使用1_导入OpenCV并依赖该项目
- eclipse开发android