Sur la base du livre technique, j'ai créé une simple application d'affichage de carte.
MainActivity.java
package com.example.implicintintentsample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class MainActivity extends AppCompatActivity {
//Obtenez des informations de latitude et de longitude à partir de votre position actuelle
private double _latitude = 0;
private double _longitude = 0;
private TextView _tvLatitude;
private TextView _tvLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Obtenez le contenu du champ TextView qui affiche la latitude et la longitude.
_tvLatitude = findViewById(R.id.tvLatitude);
_tvLongitude = findViewById(R.id.tvLongitude);
//Obtenir l'objet LocationManager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//Générer un objet écouteur lorsque les informations de position sont mises à jour
GPSLocationListener locationListener = new GPSLocationListener();
//Commencer le suivi des informations de localisation
if (ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
ActivityCompat.requestPermissions(MainActivity.this,permissions, 1000);
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
//Que se passe-t-il lorsque vous appuyez sur le bouton de recherche de carte
public void onMapSearchButtonClick(View view) {
EditText etSearchWord = findViewById(R.id.etSearchWord);
String searchWord = etSearchWord.getText().toString();
try{
searchWord = URLEncoder.encode(searchWord, "UTF-8");
String uriStr = "geo:0,0?q=" + searchWord;
Uri uri = Uri.parse(uriStr);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
catch(UnsupportedEncodingException ex){
Log.e("IntentStartActivity", "Échec de la conversion des mots clés de recherche", ex);
}
}
//Que se passe-t-il lorsque vous appuyez sur le bouton d'affichage de la carte
public void onMapShowCurrentButtonClick(View view){
String uriStr = "geo:" + _latitude + "," + _longitude;
Uri uri = Uri.parse(uriStr);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
private class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location){
_latitude = location.getLatitude();
_longitude = location.getLongitude();
_tvLatitude.setText(Double.toString(_latitude));
_tvLongitude.setText(Double.toString(_longitude));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){}
@Override
public void onProviderEnabled(String provider){}
@Override
public void onProviderDisabled(String provider){}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
if(requestCode == 1000 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
GPSLocationListener locationListener = new GPSLocationListener();
if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.implicintintentsample">
<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>
strings.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<string name="app_name">Application de carte</string>
<string name="bt_map_search">Recherche de carte</string>
<string name="tv_current_title">Localisation actuelle</string>
<string name="tv_latitude_title">latitude:</string>
<string name="tv_longitude_title">longitude:</string>
<string name="bt_map_current">Affichage de la carte</string>
</resources>
activity_main.xml
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/etSearchWord"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType=""
android:autofillHints="" />
<Button
android:id="@+id/btMapSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onMapSearchButtonClick"
android:text="@string/bt_map_search"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="@string/tv_current_title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="@string/tv_latitude_title"
/>
<TextView
android:id="@+id/tvLatitude"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="0.5"
android:maxLines="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="@string/tv_longitude_title"
/>
<TextView
android:id="@+id/tvLongitude"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:maxLines="1"
/>
<Button
android:id="@+id/btMapShowCurrent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onMapShowCurrentButtonClick"
android:text="@string/bt_map_current"
/>
</LinearLayout>
</LinearLayout>
Recommended Posts