试用Android定位服务
我试图使用我找到的示例代码()来了解位置服务是如何工作的。我的代码可以编译,但是当应用程序运行时,我似乎无法通过某个部分。下面是我正在使用的代码试用Android定位服务,android,android-location,Android,Android Location,我试图使用我找到的示例代码()来了解位置服务是如何工作的。我的代码可以编译,但是当应用程序运行时,我似乎无法通过某个部分。下面是我正在使用的代码 package com.example.gpstest; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android
package com.example.gpstest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {
private EditText editTextShowLocation;
private Button buttonGetLocation;
private ProgressBar progress;
private LocationManager locManager;
private LocationListener locListener = new MyLocationListener();
private boolean gps_enabled = false;
private boolean network_enabled = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);
progress = (ProgressBar) findViewById(R.id.progressBar1);
progress.setVisibility(View.GONE);
buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);
buttonGetLocation.setOnClickListener(this);
locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled) {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("Attention!");
builder.setMessage("Sorry, location is not determined. Please enable location providers");
builder.setPositiveButton("OK", this);
builder.setNeutralButton("Cancel", this);
builder.create().show();
progress.setVisibility(View.GONE);
}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// This needs to stop getting the location data and save the battery power.
locManager.removeUpdates(locListener);
String londitude = "Londitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
String altitiude = "Altitiude: " + location.getAltitude();
String accuracy = "Accuracy: " + location.getAccuracy();
String time = "Time: " + location.getTime();
editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + time);
progress.setVisibility(View.GONE);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_NEUTRAL){
editTextShowLocation.setText("Sorry, location is not determined. To fix this please enable location providers");
}else if (which == DialogInterface.BUTTON_POSITIVE) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}
}
应用程序将无法通过此if语句(我一直收到消息“对不起,位置不是d…”,它将退出):
原声明如下:
if (!gps_enabled && !network_enabled)
然而,我无法编译它(eclipse不喜欢所有的“AMP”),所以我对它试图做什么做了最好的猜测,但我认为我破坏了它的含义
如果有人能帮我让这段代码正常工作,或者给我指出一个不同的例子的方向,这样我就可以使用它,那将是非常有帮助的
编辑我添加了所有适当的选项:访问粗略位置、访问精细位置和互联网(只是为了好玩,因为我几乎肯定这段代码不需要互联网)
edit将我的代码中的“&”更改为“&&”
手动编辑添加的权限,现在一切正常(我最初使用GUI选项):
看起来您可能需要短路和操作员
&&
,而不是
按位AND运算符
&
。我将您的代码复制到一个新项目中,将两个必需的权限添加到清单中,并使用Jim建议的&
替换。它在我的手机上很好用
我认为您的清单中的权限设置不正确,或者您的手机没有为您的应用正确启用GPS
forisProviderEnabled
声明“如果用户在设置菜单中启用了此提供程序,则返回true,否则返回false”。我知道的唯一例外是,如果应用程序未声明所需的权限,则该方法始终返回false
如果你在模拟器上或者你的GPS被禁用了,这就可以解释了。尝试在现有的基于位置的应用程序(如谷歌地图)中获取您的当前位置。吉姆·刘易斯给了您正确的更正。如果你想避免将来犯这样的错误,我建议你在纠正不起作用时进行练习,这是不够的信息。请让我们知道在你改正后它给你的新错误。它仍然给出相同的错误:它不会超过if声明,即使所有的位置信息都在我的手机上打开了。我在另一部手机上试用了它,它做了完全相同的事情再次从零开始剪切和粘贴所有内容,但这次,将它作为中介放在一个简单的文本编辑器中,以摆脱任何其他奇怪的逃逸html实体。然后执行“项目”>“清理”并重建所有内容。在再次尝试重建之前,请务必清除错误日志,以确保您确实收到了新的错误,而不仅仅是读取旧的错误。谷歌地图工作正常,因此我猜我的位置服务实际上不太可能关闭。我的GPS接收器是,但似乎在我运行此应用程序后关闭。您添加了哪些权限?直接在
标签中,我添加了这两个权限:
和
这很令人沮丧,我按照您所说的做了,并手动添加了:而且它有效我很高兴它有效!不过我很好奇,你以前没有进入过那里的位置吗?我也很好奇是否有必要进入MOCK的位置。MOCK的位置不是。我回到我在问题中列出的网站,只是不小心粘贴了它。我第一次测试它时,它还在里面;然后我回去删除了它,它仍然有效;记住,这是在电话上,而不是在模拟器上。我觉得很奇怪,我不得不进入xml文件并添加它,而当我最初添加它们时,我使用GUI
if (!gps_enabled && !network_enabled)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />