Модель моего телефона Android # - Sony Z3 Compact Japanese (SO-O2G)
Карты Google и все другие приложения, связанные с местоположением Отлично работает на моем устройстве
Но я хочу разработать свое собственное приложение на основе определения местоположения, и на первом этапе разработки мой код не работает .. Я не знаю, в чем проблема в моем коде. Метод onLocationChange () не запущен .. Даже не могу найти мое текущее местоположение
Код манифеста
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testingpurpose">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.location.gps"/>
<uses-feature android:name="android.hardware.location.network"/>
<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>
MainActivity.java
package com.testingpurpose;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private LocationManager locationManager;
private Location lastLocation;
private double distanceIntoMeters;
private final String PERMISSION_ONE = Manifest.permission.ACCESS_FINE_LOCATION;
private final String PERMISSION_TWO = Manifest.permission.ACCESS_COARSE_LOCATION;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this,PERMISSION_ONE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this,PERMISSION_TWO ) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{PERMISSION_ONE, PERMISSION_TWO}, 0);
}
}
@Override
protected void onStart() {
super.onStart();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if ( locationManager != null ){
if (ContextCompat.checkSelfPermission(this,PERMISSION_ONE) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this,PERMISSION_TWO ) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
}
else{
Toast.makeText(this,"Permission not Granted",Toast.LENGTH_SHORT).show();
}
periodicallyCheck();
}
void periodicallyCheck(){
final Handler handler = new Handler();
final TextView tv = findViewById(R.id.tv);
handler.post(new Runnable() {
@Override
public void run() {
String format = String.format("%.2f %s",distanceIntoMeters,"meters");
tv.setText(format);
handler.postDelayed(this,1000);
}
});
}
private LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if ( lastLocation == null ){
lastLocation = location;
}
Log.i("Location Updates: ",">>> Lat "+location.getLatitude()+" , >>> Long "+location.getLongitude());
distanceIntoMeters = distanceIntoMeters + location.distanceTo(lastLocation);
lastLocation = location;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (locationManager != null && listener != null){
locationManager.removeUpdates(listener);
}
}
}
activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>