2011-07-15 156 views
5

我想在Android上做一个简单的应用程序来获取当前位置使用GPS或网络提供商(无线或塔),但我面临的问题,同时试图使其在Android上运行。它不会启动它给了我一个例外,我不知道为什么。如何以编程方式获取android中的当前位置?

这是代码。它是如此简单,因为我想测试得到的位置&以后我会添加一些功能&功能..

主:

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); 

    } 

} 

清单:

<?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 &其他站点)。

非常感谢。

注意补充:

**我真的weared的事情。我尝试使用eclipse的方式运行应用程序(通过USB连接我的设备&运行应用程序到设备而不是应用程序)。 当我尝试这种方式时,应用程序运行没问题!同时使用旧的方式(通过电子邮件发送.apk &然后从手机安装它,它不断给我例外!!) 可以任何人解释我为什么会这样?我错过了什么?因为我知道在android开发中。所以,我怕我错过了一个点...

注意: 我通过电子邮件发送bin文件夹中的.apk文件(用eclipse签名,用于测试的目的)...我试着用另一个简单的应用程序来确保.apk文件在bin文件夹中有正确的onw(用于测试)或不可用,&它可以工作..所以,apk文件是很好......但为什么网络应用程序不工作,而在日食它工作? 预先感谢;)**

解决;)

对于i在清单中前一许可添加在应用程序代码中的问题

机器人的溶液:权限=“机器人.permission.ACCESS_FINE_LOCATION“

它是重复的,因为它已被添加到下面(或因为它在错误的地方)。 所以,我删除它&它运行良好。

非常感谢你们

+0

请与堆栈跟踪发布日志,或至少告诉我们什么异常做你和确切位置。 – MByD

+0

你会得到什么例外? – jamapag

+1

是的,访问Android的GPS可能很烦人。你知道你得到了什么样的例外吗?在Eclipse中运行调试器或使用'adb logcat'(adb位于Android SDK的platform-tools目录中)。 –

回答

1

你可能丢失网络许可,您的清单

<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" /> 
+0

不,我试图把它们..但同样的问题仍然存在:( – Q8Y

+0

谢谢你的提示人...问题是从minafist;) – Q8Y

1

我觉得你可以阅读和观看这是2011 IO通过Reito迈耶期间提供的内容。

谷歌IO 2011从梅尔(观看视频,获得幻灯片,获取代码片段...):
http://blog.radioactiveyak.com/2011/05/android-protips-where-to-download.html

Reito迈耶的出版有关基于位置的应用程序:
http://blog.radioactiveyak.com/2011/06/how-to-build-location-based-apps-that.html
http://blog.radioactiveyak.com/2011/06/deep-dive-into-location-part-2-being.html

4

首先,我检查启用了哪些提供程序。有些可能在设备上被禁用,有些可能在应用程序清单中被禁用。 如果有任何提供者可用,我启动位置侦听器和超时定时器。在我的例子中是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" /> 
+0

请检查我的新帖子在主帖;)谢谢 – Q8Y

+0

你的代码或你从http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a ? – DannyThunder

相关问题