how to identify on command line terminal emulator apps and packages that have active network connections I tried this using an app and I am able to identify most apps and their  network connections however there are certain apps listed as  '?' so apparently it is not able to identify a package name I would like to know how I can get this information on command line using termux or any other emulator. I am magiskaly  rooted on an Android 10   phone
also would like to get a list of apps that can modify contacts and then revoke modification of contacts from certain apps by command line

and on command line how do I force kill an app I mean I can use the hang -9 Signal ....but I am sure there must be a more gentle package-level way of force stopping an app title the equivalent of getting inside the app property on gui and clicking that force stop button

also delete the cache on command line
 
what I want to do is write a `batch` script that will overwrite certain app specific files that the app keeps resetting every time and then force stop and clear `cache` and restart the app
If you mean by network connection the applications that has &quot;android.permission.INTERNET&quot; permission, then run this shell script:

for package in $(pm list packages -3 | cut -f2 -d&quot;:&quot;); do
dumpsys package $package | grep &quot;android.permission.INTERNET: granted=true&quot; &gt; /dev/null &amp;&amp; echo $package
done;


If you want to list all applications that are connected to internet:

netstat -tuplna

That will list all application that has an established connection

If you want for each App an their mobile data usage:

for package in $(pm list packages | cut -f2 -d&quot;:&quot;); do echo $package; pm dump $package | grep  &quot;Mobile network&quot;; done;


To kill an App:

am force-stop PACKAGE_NAME


Clear cache:

pm clear PACKAGE_NAME