Android में GPS स्थिति जांचने और सेटिंग्स खोलने का गाइड
महत्वपूर्ण: सुरक्षा कारणों से Android ऐप्स को GPS सीधे सक्षम/अक्षम करने की अनुमति नहीं देता (Android 2.3+ से)। यह ट्यूटोरियल GPS स्थिति जांचने और उपयोगकर्ता को लोकेशन सेटिंग्स स्क्रीन पर ले जाने का सही तरीका दिखाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।
चरण 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">
<Button
android:id="@+id/buttonGps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Open GPS Settings"/>
<TextView
android:id="@+id/textView"
android:layout_centerInParent="true"
android:layout_marginBottom="20dp"
android:layout_above="@id/buttonGps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Location Services" />
</RelativeLayout>
चरण 3: MainActivity.java में कोड जोड़ें
src/MainActivity.java में:
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button buttonGps;
Context context;
LocationManager locationManager;
boolean gpsStatus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
buttonGps = findViewById(R.id.buttonGps);
context = getApplicationContext();
checkGPSStatus();
buttonGps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
}
private void checkGPSStatus() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
gpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (gpsStatus) {
textView.setText("Location Services Is Enabled");
} else {
textView.setText("Location Services Is Disabled");
}
}
}
चरण 4: AndroidManifest.xml में अनुमति जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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>
बेहतर विकल्प: LocationRequest + SettingsClient (Google Play Services)
आधुनिक ऐप्स के लिए, Google Play Services का SettingsClient उपयोग करें जो उपयोगकर्ता को ऐप छोड़े बिना GPS सक्षम करने का डायलॉग दिखाता है:
LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(response -> { /* GPS पहले से सक्षम */ });
task.addOnFailureListener(e -> {
if (e instanceof ResolvableApiException) {
try { ((ResolvableApiException) e).startResolutionForResult(this, REQUEST_CHECK_SETTINGS); }
catch (IntentSender.SendIntentException ex) { /* अनदेखा */ }
}
});
ऐप चलाएं
डिवाइस कनेक्ट करें, Run आइकन पर क्लिक करें, GPS स्थिति देखें और बटन दबाकर सेटिंग्स खोलें।