2011-04-26 37 views
0

我正在尋找最優化的方式來獲得用戶的位置,我發現樣本例子在這裏,這是回答了費多爾先生在06月30日'10應用已在Android模擬器意外停止

我做了同樣的正如他在他的代碼中解釋的,唯一的區別是我使用抽象類結果的gotLocation回調方法。在這個方法中,我打算使用Toast.makeText將Provider名稱顯示爲msg。當我運行這段代碼時,沒有任何東西顯示在我的模擬器上,幾秒鐘後顯示消息「應用程序意外停止了android模擬器」。我增加了在timer1.schedule方法中設置的時間,但沒有運氣。

我只是說在android平臺上的開發,所以我沒有足夠的知識,所以任何人都可以幫助我解決這個問題。

下面是我的代碼

文件名稱:UserLocation.java

package com.ideafarms.android.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 UserLocation{ 
    Timer timer1; 
    LocationManager locMgr; 
    LocationResult locationResult; 
    boolean gps_enabled = false; 
    boolean network_enabled = false; 

    LocationListener locationListenerGps = new LocationListener() { 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
      timer1.cancel(); 
      locationResult.gotLocation(location); 
      locMgr.removeUpdates(this); 
      locMgr.removeUpdates(locationListenerNetwork); 
     } 
    }; 

    LocationListener locationListenerNetwork = new LocationListener() { 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
      timer1.cancel(); 
      locationResult.gotLocation(location); 
      locMgr.removeUpdates(this); 
      locMgr.removeUpdates(locationListenerGps);   
     } 
    }; 

    public boolean getLocation(Context context, LocationResult result){ 
     // Use LocationResult callback class to pass location value from UserLocation to User code 
     locationResult = result; 
     if(locMgr == null){ 
      locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
      // Handle exception if provider is not permitted 
      try{ 
       gps_enabled = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER); 
      }catch(Exception ex){ 

      } 
      try{ 
       network_enabled = locMgr.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){     
       locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); 
      } 
      if(network_enabled){     
       locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); 
      } 
      timer1 = new Timer(); 
      timer1.schedule(new GetLastLocation(), 20000); 
      return true; 
     } 
     return true; 
    } 
    class GetLastLocation extends TimerTask { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      locMgr.removeUpdates(locationListenerGps); 
      locMgr.removeUpdates(locationListenerNetwork); 
      Location net_loc=null, gps_loc=null; 
      if(gps_enabled){ 
       gps_loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

      } 
      if(network_enabled){ 
       net_loc=locMgr.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); 
    } 
} 

package com.ideafarms.android.mylocation; 

import android.app.Activity; 
import android.location.Location; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.ideafarms.android.mylocation.UserLocation.LocationResult; 

public class MyLocation extends Activity { 

    /** Called when the activity is first created. */ 
    TextView myLoc ; 
    public LocationResult locResult = new LocationResult(){ 
     @Override 
     *public void gotLocation(Location location) { 
      // TODO Auto-generated method stub 

      Toast msg = Toast.makeText(MyLocation.this, location.getProvider(), Toast.LENGTH_LONG); 
      //msg.setGravity(Gravity.CENTER, msg.getXOffset()/2, msg.getYOffset()/2); 
      msg.show(); 
     }*  
    }; 


    boolean loc; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     UserLocation usrLocation = new UserLocation(); 
     myLoc = (TextView)findViewById(R.id.myLocation); 
     loc = usrLocation.getLocation(this, locResult); 

    } 

} 

我已標記在我有問題斜體的代碼。

感謝

+0

這是很難通過查看整個程序跟蹤你的問題..... – vnshetty 2011-04-26 10:02:23

回答

0

看看這裏:http://developer.android.com/guide/developing/tools/emulator.html

你需要爲了從模擬器獲得位置數據來模擬一個GPS設備。

+0

此外,他需要知道在哪裏可以找到logcat ... – WarrenFaith 2011-04-26 08:21:43

+0

感謝您的迴應。儘管我已經在console ::> telnet localhost 5554上使用以下語法完成了此操作:地理修復28.5543 77.302。你在談論相同還是其他一些事情?請簡單介紹一下 – 2011-04-26 10:14:31

相關問題