我正在尝试在 android 上制作一个简单的应用程序,以使用 GPS 或网络提供商(WIFI 或塔)获取当前位置,但我在尝试使其在 android 上运行时遇到了很多问题。它不会启动它给了我一个异常(exception),我不知道为什么。
这是代码。非常简单,因为我想测试获取位置,稍后我会添加一些特性和功能..
主要内容:
package com.GpsTesting;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class GPS_TestActivity extends Activity{
/** Called when the activity is first created. */
String location_text="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
location_text= "latitude: "+lat+"longitude: "+lng;
Toast.makeText(getApplicationContext(), location_text, Toast.LENGTH_SHORT).show();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
}
}
list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.GpsTesting"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:permission="android.permission.ACCESS_FINE_LOCATION">
<activity android:name=".GPS_TestActivity"
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>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
那么,是不是代码有问题??我正在使用在线教程(来自 android dev 和其他网站)来做到这一点。
非常感谢。
添加的注释:
**我真的穿过东西。我尝试使用 eclipse 方式运行应用程序(通过 USB 连接我的设备并将应用程序运行到设备而不是应用程序中).. 当我尝试这种方式时,应用程序运行正常,没有问题!!!在使用旧方法时(通过电子邮件发送 .apk,然后从手机安装它,它一直给我异常(exception)!!) 任何人都可以为我解释为什么会这样吗?我错过了什么吗? 因为,我知道 android 开发。所以,恐怕我漏掉了一点……
注意: 我正在通过电子邮件发送 bin 文件夹中的 .apk 文件(用 eclipse 签名,用于测试目的)...我尝试使用另一个简单的应用程序只是为了确保 .apk 文件是在 bin 文件夹中是否可用(用于测试)或不可用,它确实有效.. 所以,apk 文件很好......但是为什么网络应用程序在 eclipse 中它不起作用而它不起作用? 提前致谢;)**
已解决;)
我之前在manifest中的application标签中添加权限问题的解决方案
android:permission="android.permission.ACCESS_FINE_LOCATION"
它是重复的,因为它已经被添加到下面(或者因为它在错误的地方)。 所以,我删除了它,它运行良好。
非常感谢各位
最佳答案
首先,我检查启用了哪些提供程序。有些可能在设备上被禁用,有些可能在应用程序 list 中被禁用。 如果有可用的提供程序,我将启动位置监听器和超时计时器。在我的例子中是 20 秒,对于 GPS 来说可能不够,所以你可以放大它。 如果我从位置监听器获得更新,我将使用提供的值。我停止听众和计时器。 如果我没有得到任何更新和计时器过去,我必须使用最后已知的值。 我从可用的提供者那里获取最后已知的值并选择最近的值。
MyLocation myLocation = new MyLocation();
private void locationClick() {
myLocation.getLocation(this, locationResult);
}
public LocationResult locationResult = new LocationResult() {
@Override
public void gotLocation(final Location location) {
//Got the location!
}
};
这是 MyLocation 类:
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;
public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if(gps_loc!=null){
locationResult.gotLocation(gps_loc);
return;
}
if(net_loc!=null){
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
}
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
关于java - 如何以编程方式获取 android 中的当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6706795/