Android’de mevcut GPS konumunu programlı olarak nasıl alabilirim?

Bu örnek, Android’de güncel GPS konumunu programlı olarak nasıl alacağımı gösterir.

Bu örnek, Android’de güncel GPS konumunu programlı olarak nasıl alacağımı gösterir.

Android Studio’da yeni bir proje oluşturun, Dosya ⇒ Yeni Proje’ye gidin ve yeni bir proje oluşturmak için gerekli tüm ayrıntıları doldurun.

res / layout / activity_main.xml dosyasına aşağıdaki kodu ekleyin

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_margin="16dp"
   android:gravity="center_horizontal"
   android:orientation="vertical">
   <TextView
      android:id="@+id/location"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Current GPS Loaction"
      android:textSize="24sp"
      android:textStyle="bold" />
   <Button
      android:id="@+id/getLocation"
      android:text="Get location"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

src / MainActivity.java dizinine aşağıdaki kodu ekleyin

import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
public class MainActivity extends AppCompatActivity {
   private FusedLocationProviderClient client;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      requestPermission();
      client = LocationServices.getFusedLocationProviderClient(this);
      Button button = findViewById(R.id.getLocation);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
               return;
            }
            client.getLastLocation().addOnSuccessListener(MainActivity.this, new OnSuccessListener() {
               @Override
               public void onSuccess(Location location) {
                  if (location != null) {
                     TextView textView = findViewById(R.id.location);
                     textView.setText(location.toString());
                  }
               }
            });
         }
      });
   }
   private void requestPermission(){
      ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 1);
   }
}

androidManifest.xml dosyasına aşağıdaki kodu ekleyin

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://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>

Uygulamanızı çalıştırmayı deneyelim. Gerçek Android Mobil cihazınızı bilgisayarınıza bağladığınızı varsayıyorum. Uygulamayı android studio’dan çalıştırmak için projenizin etkinlik dosyalarından birini açın 

 ve araç çubuğundan Çalıştır simgesini tıklayın . Mobil cihazınızı seçenek olarak seçin ve ardından varsayılan ekranınızı görüntüleyecek mobil cihazınızı kontrol edin.

Comments

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

GIPHY App Key not set. Please check settings

Yükleniyor…

0

Ne düşünüyorsun?

Ulaştırma Elektronik Takip ve Denetim Sistemi (U-ETDS), ilk olarak 08.01.2018 tarih ve 30295 sayılı Resmi Gazete’de yayımlanarak yürürlüğe giren Karayolu Taşıma Yönetmeliği (KTY) ile tanımlanmıştır.

U-ETDS Nedir?

Geri çağırma, 2018 ve 2019 model yıllarından birkaç hibrit olmayan Toyota ve Lexus modelini içeriyor.

Toyota, yakıt pompası sorununda yaklaşık 700.000 aracı geri çağırıyor