android: Add a text field to display the devices network address
This commit is contained in:
31
android/app/src/main/java/org/musicpd/NetworkUtil.java
Normal file
31
android/app/src/main/java/org/musicpd/NetworkUtil.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package org.musicpd;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.LinkAddress;
|
||||
import android.net.LinkProperties;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.util.List;
|
||||
|
||||
public class NetworkUtil {
|
||||
|
||||
public static String getDeviceIPV4Address(Context context) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
LinkProperties linkProperties = connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork());
|
||||
if (linkProperties != null) {
|
||||
List<LinkAddress> linkAddresses = linkProperties.getLinkAddresses();
|
||||
for (LinkAddress address : linkAddresses) {
|
||||
if (!address.getAddress().isLinkLocalAddress() && !address.getAddress().isLoopbackAddress()) {
|
||||
InetAddress address1 = address.getAddress();
|
||||
if (address1 instanceof Inet4Address) {
|
||||
return address1.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -213,6 +213,10 @@ public class Settings extends Activity {
|
||||
if (Preferences.getBoolean(this, Preferences.KEY_PAUSE_ON_HEADPHONES_DISCONNECT, false))
|
||||
checkbox.setChecked(true);
|
||||
|
||||
TextView networkAddressTextView = (TextView) findViewById(R.id.networkAddress);
|
||||
String deviceIPV4Address = NetworkUtil.getDeviceIPV4Address(this);
|
||||
networkAddressTextView.setText(deviceIPV4Address);
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user