我只是尝试做一个给出当前位置图(经度和纬度值)的应用程序 我刚刚创建了一个 GPstracker.java 并在那里使用了操作 我只需要获得与按下按钮相关的两个值 它对我未来的编码很有帮助我是初学者 这是我的主要活动代码
package com.example.shellz.gps_locator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn;
GPStracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btn=(Button)findViewById(R.id.location_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gps=new GPStracker(MainActivity.this);
if(gps.canGetLocation())
{
Double latitude=gps.getLatitude();
Double longitude=gps.getLongitude();
Toast.makeText(getApplicationContext(),latitude+" and "+longitude+" ",Toast.LENGTH_LONG).show();
}
else
{
gps.showSettingsAlert();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的 GPStracker.java 这里我做了与我的项目相关的所有操作
package com.example.shellz.gps_locator;
import android.Manifest;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
public class GPStracker extends Service implements LocationListener {
private static final Location TODO = null;
private final Context mContext;
//gps status
Boolean isGPSEnable = false;
//network status
Boolean isNetwokEnable = false;
Boolean CangetLoaction = false;
Location location;
Double latitude;
Double longitude;
private static final long MIN_DISTANCE_FOR_UPDATE = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationmanger;
public GPStracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
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 TODO;
}
locationmanger = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnable = locationmanger.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetwokEnable = locationmanger.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isNetwokEnable && !isGPSEnable) {
Toast.makeText(getApplicationContext(), "No Network Avialiable", Toast.LENGTH_LONG).show();
} else {
this.CangetLoaction = true;
if (isNetwokEnable) {
locationmanger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_FOR_UPDATE, this);
if (locationmanger != null) {
location = locationmanger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnable) {
locationmanger.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_FOR_UPDATE, this);
if (locationmanger != null) {
location = locationmanger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS() {
if (locationmanger != null) {
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;
}
locationmanger.removeUpdates(GPStracker.this);
}
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation();
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这里是我在运行时得到的错误
06-25 17:14:36.296 10750-10750/com.example.shellz.gps_locator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.StackOverflowError
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.java:147)
at com.example.shellz.gps_locator.GPStracker.canGetLocation(GPStracker.
答案 0 :(得分:3)
这是因为函数调用自身。它是一个无限循环。
public boolean canGetLocation() {
return this.canGetLocation();
}
修改此功能以删除此错误。
这个想你想把它改成:
public boolean canGetLocation() {
return this.CangetLoaction;
}