परिचय
यह उदाहरण दर्शाता है कि Android डिवाइस के सिस्टम वर्जन (Android संस्करण नाम और API लेवल) को प्रोग्रामेटिक रूप से कैसे जांचा जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML
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"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: MainActivity.java में वर्जन डिटेक्शन लॉजिक
src/MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// रिलीज वर्जन स्ट्रिंग से मेजर/माइनर वर्जन निकालें (जैसे "7.1.1" → 7.1)
double release = Double.parseDouble(Build.VERSION.RELEASE.replaceAll("(\\d+[.]\\d+)(.*)", "$1"));
String codeName = "असमर्थित";
if (release >= 4.1 && release < 4.4) codeName = "जेली बीन";
else if (release < 5) codeName = "किटकैट";
else if (release < 6) codeName = "लॉलीपॉप";
else if (release < 7) codeName = "मार्शमैलो";
else if (release < 8) codeName = "नूगा";
else if (release < 9) codeName = "ओरियो";
// नए संस्करणों के लिए और शर्तें जोड़ सकते हैं
textView.setText(codeName + " वर्जन " + release + ", API लेवल: " + Build.VERSION.SDK_INT);
}
}
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
ऐप चलाएं
ऐप चलाने पर स्क्रीन पर Android संस्करण का नाम (जैसे "नूगा"), वर्जन नंबर, और API लेवल प्रदर्शित होगा।
