我又对我们的一个学校项目提出了另一个问题。这次,我们想做类似Android的事件策划器之类的事情。我看过很多这样的帖子,但似乎没有一个对我有帮助。我正在寻找一种简单的解决方案,以通过按钮获取当前的经度,纬度和经度,以将其保存在变量中,但是如果我在“ getCurrentLocation();”处设置调试点,请启动我的应用程序,然后单击按钮my即使我取消对gpsEnabled和networkEnabled标志的注释,IDE也会在调试完成后调试两个try catch。我的错在哪里
这是我的 MainActivity.java
package com.example.simplevents;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
protected LocationManager locationManager;
protected Location currentLocation;
protected Button retrieveLocationButton;
public double currentGPSLatitude;
public double currentGPSLongitude;
public double currentGPSAltitude;
public double currentNETLatitude;
public double currentNETLongitude;
public double currentNETAltitude;
protected boolean gpsEnabled;
protected boolean networkEnabled;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
retrieveLocationButton = findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getCurrentLocation();
}
});
}
public void getCurrentLocation() {
try {
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception e) {
e.printStackTrace();
}
try {
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception e) {
e.printStackTrace();
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (gpsEnabled) {
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
currentGPSLongitude = currentLocation.getLongitude();
currentGPSLatitude = currentLocation.getLatitude();
currentGPSAltitude = currentLocation.getAltitude();
} else if (networkEnabled) {
currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
currentNETLongitude = currentLocation.getLongitude();
currentNETLatitude = currentLocation.getLatitude();
currentNETAltitude = currentLocation.getAltitude();
}
Toast.makeText(MainActivity.this, "lat:" + currentNETLatitude + "long:" + currentNETLongitude + "alt:" + currentNETAltitude,
Toast.LENGTH_LONG).show();
}
}
activity_main.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"
android:visibility="visible"
tools:context=".MainActivity">
<Button
android:id="@+id/retrieve_location_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="96dp"
android:layout_marginRight="96dp"
android:clickable="true"
android:focusable="true"
android:visibility="visible"
app:backgroundTint="@android:color/darker_gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_compass" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="51dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
androidmanifest.xml
<?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-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
预先感谢