我一整天都在寻找解决问题的方法,但我找不到。我是java / android studio / app编程的新手,可能它只是一个我看不到的小东西。
我只想让我的应用程序显示设备的当前位置。没什么。
在模拟器上运行App时,不会显示任何错误,应用程序将启动。
然后我打开扩展控件并按“位置”中的“SEND”按钮。 现在我希望我的应用程序显示当前位置,但没有任何反应。 TextViews“textLat”,“textLong”和“textAlt”保持空白。
我绝对不知道我的错是什么。我在不同的教程的帮助下编写了该代码。我也在我的真实设备上尝试过它。
我会非常感谢任何帮助!非常感谢!
package com.example.findlocation_test2;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView textLat;
TextView textLong;
TextView textAlt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
textAlt = (TextView) findViewById(R.id.textAlt);
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if(location != null) {
double dLat = location.getLatitude();
double dLong = location.getLongitude();
double dAlt = location.getAltitude();
textLat.setText(Double.toString(dLat));
textLong.setText(Double.toString(dLong));
textAlt.setText(Double.toString(dAlt));
}
else {
textLat.setText("Fehler");
textLong.setText("Fehler");
textAlt.setText("Fehler");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(), "GPS wurde akiviert",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
Toast.makeText(getBaseContext(), "GPS wurde deaktiviert",
Toast.LENGTH_SHORT).show();
}
};
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;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, ll);
}
}
在清单中我添加了权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.findlocation_test2">
<uses-permission android:name="android.permission.ACCESS_INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> /**Erlaubnis, um auf die GPS Daten zuzugreifen*/
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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>
</manifest>