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

Android में GPS को प्रोग्रामेटिकली कैसे सक्षम/अक्षम करें?

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 स्थिति देखें और बटन दबाकर सेटिंग्स खोलें।

  1. Android में ScrollView को प्रोग्रामेटिकली कैसे डिसेबल करें? Android में ScrollView को प्रोग्रामेटिकली कैसे डिसेबल करें?

    परिचय यह उदाहरण दर्शाता है कि Android में ScrollView को प्रोग्रामेटिक रूप से कैसे डिसेबल किया जाए। हम OnTouchListener का उपयोग करके टच इवेंट को इंटरसेप्ट करेंगे। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project के माध्यम से नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML में ScrollView res/l

  1. Android पर Google Feed इनेबल या डिसेबल करें: चरण-दर-चरण गाइड Android पर Google Feed इनेबल या डिसेबल करें: चरण-दर-चरण गाइड

    Google Feed Google की एक बहुत ही दिलचस्प और उपयोगी विशेषता है। यह आपकी रुचियों के आधार पर विशेष रूप से आपके लिए क्यूरेट की गई खबरों और सूचनाओं का संग्रह है। Google Feed आपको उन कहानियों और समाचार स्निपेट्स के साथ प्रदान करता है जो आपके लिए आकर्षक हो सकते हैं। उदाहरण के लिए, आपके द्वारा फॉलो की जाने

  1. एंड्रॉइड पर ऐप आइकन बैज इनेबल या डिसेबल कैसे करें: स्टेप-बाय-स्टेप गाइड एंड्रॉइड पर ऐप आइकन बैज इनेबल या डिसेबल कैसे करें: स्टेप-बाय-स्टेप गाइड

    अगर आप एंड्रॉइड फोन का उपयोग कर रहे हैं, तो आप जानते हैं कि जब आप नोटिफिकेशन प्राप्त करते हैं, तो आपका फोन उन्हें लॉक स्क्रीन पर अलर्ट के रूप में प्रदर्शित करता है। आप आसानी से अनलॉक करके नोटिफिकेशन शेड को नीचे स्क्रॉल करके नोटिफिकेशन देख सकते हैं। इसके अलावा, आप अपने एंड्रॉइड फोन पर नोटिफिकेशन अलर्