Dies ist mein Android-Projekt. Die App zeigt Informationen zum aktuellen Standort an.
Die App zeigt jedoch immer an, dass der Standort null ist. Ich habe viel gesucht, konnte aber die Lösung nicht finden oder konnte die Lösung nicht implementieren.Die Position ist immer Null
Hier ist mein MainActivity Code,
package com.learning.pranavjain.hikerswatch;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends Activity implements LocationListener {
LocationManager locationManager;
String provider;
String sLatitude;
String sLongitude;
String sAccuracy;
String sSpeed;
String sAltitude;
TextView addressTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Hiding the status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(),false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider,400,1,this);
location = locationManager.getLastKnownLocation(provider);
if(location==null){
Log.i("Location1","NULL1");
Toast.makeText(this,"Location not found",Toast.LENGTH_SHORT).show();
}
if(location!=null) {
onLocationChanged(location);
Log.i("Location1","Reached here");
}
}
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
Float acc = location.getAccuracy();
Float spe = location.getSpeed();
Double alt = location.getAltitude();
Log.i("Location","reached here");
TextView latitude = (TextView) findViewById(R.id.latitude);
TextView longitude = (TextView) findViewById(R.id.longitude);
TextView accuracy = (TextView) findViewById(R.id.accuracy);
TextView speed = (TextView) findViewById(R.id.speed);
TextView altitude = (TextView) findViewById(R.id.altitude);
addressTV = (TextView) findViewById(R.id.address);
sLatitude = latitude.getText().toString();
sLongitude = longitude.getText().toString();
sAccuracy = accuracy.getText().toString();
sSpeed = speed.getText().toString();
sAltitude = altitude.getText().toString();
sLatitude += Double.toString(lat);
sLongitude += Double.toString(lng);
sAccuracy += Double.toString(acc);
sSpeed += Double.toString(spe);
sAltitude += Double.toString(alt);
latitude.setText(sLatitude);
longitude.setText(sLongitude);
accuracy.setText(sAccuracy);
speed.setText(sSpeed);
altitude.setText(sAltitude);
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(lat, lng, 1);
if (listAddresses != null && listAddresses.size() > 0) {
Log.i("PlaceInfo", listAddresses.get(0).toString());
String addressHolder = "";
for (int i = 0; i <= listAddresses.get(0).getMaxAddressLineIndex(); i++) {
addressHolder += listAddresses.get(0).getAddressLine(i) + "\n";
}
addressTV.setText("Address:\n" + addressHolder);
}
} catch (IOException e) {
e.printStackTrace();
}
Log.i("Location:", String.valueOf(lat));
Log.i("Location:", lng.toString());
}
@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 onPause() {
super.onPause();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.removeUpdates(this);
}
}
Hier ist mein Manifest Code,
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.location.gps" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
Das gesamte Projekt bei here (GitHub) gefunden werden kann
getLastKnownLocation kann null zurückgeben Sie sollten eine Standortaktualisierung anfordern, wenn dies der Fall ist. – Raghunandan
'Betrachten Sie Calling ActivityCompat # requestPermissions' Sie haben eigentlich * tun *, wenn Sie auf Android M und höher laufen – njzk2
@Raghunandan Ich habe das bereits getan –