2012-07-04 62 views
0

我正在開發小型Android應用程序,我在其中查找用戶當前位置。 我的用於檢測用戶位置的代碼結構看起來像。無法檢測到用戶在Android設備上的當前位置

private void sendSMS(Context context, Intent intent) 
{ 
    final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters 
    final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds 

    LocationManager locationManager; 
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

    locationManager.requestLocationUpdates(
     LocationManager.GPS_PROVIDER, 
    MINIMUM_TIME_BETWEEN_UPDATES, 
    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
    new MyLocationListener() 
    ); 

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    String loc_message = null; 

    if (location != null) 
    { 
    loc_message =String.format(
     "Current Location \n Longitude: %1$s \n Latitude: %2$s", 
     location.getLongitude(), location.getLatitude() 
    ); 
    Toast.makeText(context, loc_message,Toast.LENGTH_LONG).show(); 
    } 
} 
private class MyLocationListener implements LocationListener { 

     public void onLocationChanged(Location location) { 
     String message = String.format(
       "New Location \n Longitude: %1$s \n Latitude: %2$s", 
       location.getLongitude(), location.getLatitude() 
     ); 
    } 

    public void onStatusChanged(String s, int i, Bundle b) { 

    } 

    public void onProviderDisabled(String s) { 
    } 

    public void onProviderEnabled(String s) { 
    } 
    } 
} 

在我從DDMS發送座標的模擬器上正常工作。但是當我在設備上運行它不會給我輸出。在我的設備上,我保持使用GPS衛星啓用。但是當我試圖找到用戶的位置它沒有給任何輸出....需要幫助...謝謝.......

+0

請注意,根據您的位置獲取GPS座標可能需要幾分鐘的時間,但如果您的手機看不到足夠的衛星,則根本無法獲得定位。這可能是因爲你在室內,或者因爲天氣不好。我建議你要耐心,也最好是在活動的'onCreate(...)'方法中註冊LocationListener,並在'onDestroy(...)'中註銷它。繼續檢查LogCat中的日誌,你將能夠發現你是否得到了修復。 –

+0

哪個真正的設備,你正在使用我,E哪些公司..........因爲一些中國標籤不支持GPS,而你必須使用網絡或WiFi提供商的....... ..... –

+0

謝謝你的答覆。我使用samsunge(android 2.1)。 – nilkash

回答

1

這裏是GPS的骨架Service我用我的跟蹤應用程序,它已經過測試,並且工作正常,我可以保證。我希望它能幫助你。

import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Notification; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.IBinder; 
import android.os.PowerManager; 
import android.os.PowerManager.WakeLock; 
import android.preference.PreferenceManager; 
import android.util.Log; 

public class TrackingService extends Service { 
    private static final String TAG = TrackingService.class.getSimpleName(); 

    private static final long TIME_BETWEEN_UPDATES = 1000L; 
    private static final long MINIMUM_DISTANCE_CHANGE = 0L; 

    private WakeLock mWakeLock; 

    private LocationManager mLocationManager; 

    private final Timer mTimer = new Timer(); 
    private Handler mHandler = new Handler(); 

    private LocationListener mLocationListenerGps = new LocationListener() { 

     public void onLocationChanged(Location location) { 
      // Your code here 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onProviderDisabled(String provider) { 
     } 
    }; 

    private void registerLocationListener() { 
     if (mLocationManager == null) { 
      Log.e(TAG, "TrackingService: Do not have any location manager."); 
      return; 
     } 
     Log.d(TAG, "Preparing to register location listener w/ TrackingService..."); 
     try { 
      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE, mLocationListenerGps); 
      Log.d(TAG, "...location listener now registered w/ TrackingService @ " + TIME_BETWEEN_UPDATES); 
     } catch (RuntimeException e) { 
      Log.e(TAG, "Could not register location listener: " + e.getMessage(), e); 
     } 
    } 

    private void unregisterLocationListener() { 
     if (mLocationManager == null) { 
      Log.e(TAG, "TrackingService: Do not have any location manager."); 
      return; 
     } 
     mLocationManager.removeUpdates(mLocationListenerGps); 
     Log.d(TAG, "Location listener now unregistered w/ TrackingService."); 
    } 

    private TimerTask mCheckLocationListenerTask = new TimerTask() { 
     @Override 
     public void run() { 
      mHandler.post(new Runnable() { 
       public void run() { 
        Log.d(TAG, "Re-registering location listener with TrackingService."); 
        unregisterLocationListener(); 
        registerLocationListener(); 
       } 
      }); 
     } 
    }; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     registerLocationListener(); 

     mTimer.schedule(mCheckLocationListenerTask, 1000 * 60 * 5, 1000 * 60); 

     acquireWakeLock(); 

     Log.d(TAG, "Service started..."); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     handleStartCommand(intent, startId); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     handleStartCommand(intent, startId); 
     return START_STICKY; 
    } 

    private void handleStartCommand(Intent intent, int startId) { 
     Notification notification = new Notification(R.drawable.ic_launcher, 
       getText(R.string.trackingservice_notification_rolling_text), 
       System.currentTimeMillis()); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     notification.setLatestEventInfo(this, 
       getText(R.string.trackingservice_notification_ticker_title), 
       getText(R.string.trackingservice_notification_ticker_text), 
       contentIntent); 

     startForeground(1, notification); 
    } 

    @Override 
    public void onDestroy() { 
     stopForeground(true); 
     mTimer.cancel(); 
     mTimer.purge(); 
     mHandler.removeCallbacksAndMessages(null); 
     unregisterLocationListener(); 
     releaseWakeLock(); 
     super.onDestroy(); 
     Log.d(TAG, "Service stopped..."); 
    } 

    private void acquireWakeLock() { 
     try { 
      PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
      if (pm == null) { 
       Log.e(TAG, "TrackRecordingService: Power manager not found!"); 
       return; 
      } 
      if (mWakeLock == null) { 
       mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); 
       if (mWakeLock == null) { 
        Log.e(TAG, "TrackRecordingService: Could not create wake lock (null)."); 
        return; 
       } 
      } 
      if (!mWakeLock.isHeld()) { 
       mWakeLock.acquire(); 
       if (!mWakeLock.isHeld()) { 
        Log.e(TAG, "TrackRecordingService: Could not acquire wake lock."); 
       } 
      } 
     } catch (RuntimeException e) { 
      Log.e(TAG, "TrackRecordingService: Caught unexpected exception: " 
        + e.getMessage(), e); 
     } 
    } 

    /** 
    * Releases the wake lock if it's currently held. 
    */ 
    private void releaseWakeLock() { 
     if (mWakeLock != null && mWakeLock.isHeld()) { 
      mWakeLock.release(); 
      mWakeLock = null; 
     } 
    } 
} 

而在你的AndroidManifest.xml文件,你需要

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.WAKE_LOCK"/> 

只需填寫GPS位置監聽器,在您的AndroidManifest.xml註冊服務,從活動啓動該服務,並享受。

+0

謝謝你的答覆..在我的代碼,如果我使用網絡作爲位置提供商,那麼它的工作很好。但如果我使用gps作爲網絡提供商,它不提供任何輸出。任何建議..謝謝你的答案。我也會嘗試你的答案。 – nilkash

0

Nilkash,我經歷了同樣的問題在我的三星Galaxy 5

我得到了我的問題解決了,因爲你的建議更改爲網絡供應商。感謝那個好友。我得到了解決方案。

「網絡提供商」根據小區塔和WiFi接入點的可用性確定位置。通過網絡查找檢索結果。需要android.permission.ACCESS_COARSE_LOCATION或android.permission.ACCESS_FINE_LOCATION的權限。

但「GPS提供者」使用衛星確定位置。根據條件,此提供程序可能需要一段時間才能返回定位修復。需要權限android.permission.ACCESS_FINE_LOCATION。

這是設備的問題,我們的設備不支持沒有互聯網的GPS它只使用位置提供商。

相關問題