Acccess to /proc/net/tcp in Android Q

柔情痞子 提交于 2019-12-06 11:19:45

ConnectivityManager is a platform level API. You can try java reflection in order to access the getConnectionOwnerUid in your context. But I suggest you to update compileSdk version to the latest.

import static android.os.Process.INVALID_UID;
import static android.system.OsConstants.IPPROTO_TCP;

InetSocketAddress remoteInetSocketAddress = new InetSocketAddress(finalHost, srcPort);
InetSocketAddress localInetSocketAddress = new InetSocketAddress(1234);

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Method method = ConnectivityManager.class.getMethod("getConnectionOwnerUid", int.class, InetSocketAddress.class, InetSocketAddress.class);
int uid = (int) method.invoke(connectivityManager, IPPROTO_TCP, localInetSocketAddress, remoteInetSocketAddress);
if (uid != INVALID_UID) {
       // UID access here         
  }

NOTE: The method only allows VPN apps to lookup the UID owner of a network connection. That means only connections for UIDs that apply to the calling VPN app will be resolved.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!