Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android Emulator का IP एड्रेस कैसे प्राप्त करें?

WifiManager का उपयोग करके Android एमुलेटर/डिवाइस का स्थानीय IP एड्रेस प्रोग्रामेटिकली प्राप्त करने का तरीका।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="16sp"
        android:textStyle="bold" />
    <Button
        android:id="@+id/btnGetIPAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:text="Get IP Address" />
</RelativeLayout>

चरण 3: IP एड्रेस लॉजिक

src/MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button button;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.btnGetIPAddress);
        button.setOnClickListener(v -> getIPAddress());
    }

    private void getIPAddress() {
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        if (wifiManager != null) {
            int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
            String ip = Formatter.formatIpAddress(ipAddress);
            textView.setText(String.format("IP Address: %s", ip));
        }
    }
}

चरण 4: मैनिफेस्ट अनुमतियां

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

एमुलेटर विशेष नोट

  • एमुलेटर का डिफ़ॉल्ट IP: 10.0.2.15 (NAT के पीछे)
  • होस्ट मशीन तक पहुंचने के लिए: 10.0.2.2
  • एमुलेटर के बीच संचार: प्रत्येक का अपना 10.0.2.15 होता है
  • वास्तविक डिवाइस पर वाईफाई IP दिखेगा (जैसे 192.168.x.x)

सभी नेटवर्क इंटरफेस प्राप्त करें (बेहतर)

public List<String> getAllIpAddresses() {
    List<String> ips = new ArrayList<>();
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                    ips.add(inetAddress.getHostAddress());
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return ips;
}

अनुमतियां (API 29+)

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  1. प्रोग्रामेटिकली वर्तमान Android SDK वर्जन कैसे प्राप्त करें? प्रोग्रामेटिकली वर्तमान Android SDK वर्जन कैसे प्राप्त करें?

    Android में प्रोग्रामेटिकली SDK वर्जन जानना यह ट्यूटोरियल दिखाता है कि कैसे रनटाइम पर डिवाइस का API लेवल और Android वर्जन प्राप्त किया जाता है, जो वर्जन-स्पेसिफिक फीचर्स इम्प्लीमेंट करने के लिए जरूरी है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट में TextView res/l

  1. एंड्रॉइड में दो भौगोलिक स्थानों के बीच दूरी कैसे निकालें एंड्रॉइड में दो भौगोलिक स्थानों के बीच दूरी कैसे निकालें

    Location.distanceTo() विधि का उपयोग करके दो GPS निर्देशांकों के बीच दूरी की गणना एंड्रॉइड का android.location.Location क्लास दो भौगोलिक निर्देशांकों (अक्षांश/देशांतर) के बीच सीधी रेखा (हवाई दूरी) की गणना के लिए बिल्ट-इन distanceTo() विधि प्रदान करता है। यह हेवर्साइन फॉर्मूला का उपयोग करता है और परिण

  1. Android डिवाइस का प्राइमरी ईमेल एड्रेस कैसे प्राप्त करें? Android डिवाइस का प्राइमरी ईमेल एड्रेस कैसे प्राप्त करें?

    परिचय यह ट्यूटोरियल दिखाता है कि Android डिवाइस पर कॉन्फ़िगर किए गए अकाउंट्स में से प्राइमरी ईमेल एड्रेस कैसे प्राप्त करें। हम AccountManager और GET_ACCOUNTS परमिशन का उपयोग करेंगे। ⚠️ महत्वपूर्ण चेतावनी नोट: यह प्रोग्राम केवल रूटेड डिवाइस पर ही काम करेगा। आधुनिक Android वर्जन (Android 6.0+) में `G