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

Kotlin का उपयोग करके एंड्रॉइड एमुलेटर का IP पता कैसे प्राप्त करें?

परिचय

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके एंड्रॉइड एमुलेटर या डिवाइस का वाईफाई 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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@android:color/black"
        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="IP पता प्राप्त करें" />

</RelativeLayout>

चरण 3: MainActivity.kt में कोड जोड़ें

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

class MainActivity : AppCompatActivity() {
    private lateinit var button: Button
    private lateinit var textView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        textView = findViewById(R.id.textView)
        button = findViewById(R.id.btnGetIPAddress)

        button.setOnClickListener {
            val wifiManager = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
            val ipAddress = Formatter.formatIpAddress(wifiManager.connectionInfo.ipAddress)
            textView.text = "IP पता: $ipAddress"
        }
    }
}

चरण 4: AndroidManifest.xml में अनुमति जोड़ें

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.q11">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

महत्वपूर्ण नोट्स

  • यह केवल वाईफाई IP पता दिखाता है, मोबाइल डेटा IP नहीं
  • एमुलेटर पर, यह अक्सर 10.0.2.15 दिखाता है (एमुलेटर का आंतरिक IP)
  • वास्तविक डिवाइस पर, यह डिवाइस का वास्तविक स्थानीय IP दिखाएगा
  • Android 10+ (API 29+) पर, गैर-सिस्टम ऐप्स के लिए MAC/आईपी एक्सेस प्रतिबंधित है

ऐप चलाएं

डिवाइस/एमुलेटर पर ऐप चलाएं और "IP पता प्राप्त करें" बटन दबाएं।

  1. एंड्रॉइड डिवाइस का IP एड्रेस प्रोग्रामेटिकली कैसे प्राप्त करें? एंड्रॉइड डिवाइस का IP एड्रेस प्रोग्रामेटिकली कैसे प्राप्त करें?

    एंड्रॉइड में डिवाइस IP एड्रेस प्राप्त करना यह ट्यूटोरियल वाई-फाई और मोबाइल डेटा दोनों के लिए लोकल और पब्लिक IP एड्रेस प्राप्त करने के तरीकों को कवर करता है। आवश्यक अनुमतियाँ <uses-permission android:name=android.permission.ACCESS_WIFI_STATE /> <uses-permission android:name=android.permissi

  1. एंड्रॉइड में रिसोर्स ID से रिसोर्स नाम कैसे प्राप्त करें? एंड्रॉइड में रिसोर्स ID से रिसोर्स नाम कैसे प्राप्त करें?

    एंड्रॉइड में रिसोर्स ID से रिसोर्स नाम प्राप्त करना यह उदाहरण दिखाता है कि `getResources().getResourceName()` मेथड का उपयोग करके किसी व्यू की रिसोर्स ID से उसका पूरा नाम (पैकेज:टाइप/नाम) कैसे प्राप्त किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट

  1. Android Emulator का IP एड्रेस कैसे प्राप्त करें? 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