2016-02-12 152 views
1

在我的應用程序想嘲笑我的位置到一個給定的地方。現在,這是調用提供商和欺騙的位置到我給它的位置。它在運行函數中看到它只是一個secods。 網絡提供商和Gps提供商有什麼區別?我應該使用什麼?現在我正在使用它們,這是我的代碼:網絡提供商與GPS提供商模擬位置android

public boolean   

    startMockLocation(com.quinny898.library.persistentsearch.Location location) { 


    if (location != null && location.getLng() == -1000 && location.getLat() == -1000) { 
     try { 
      PlaceAPI placeApi = new PlaceAPI(); 
      placeApi.getLatLng(location); 
     } catch (Exception e) { 

     } 
    } 
    if (location != null) 
     if (location.getLng() != -1000 && location.getLat() != -1000) { 
      this.currentLocation = location; 
      try { 
       if(!hasProvider) { 
        LocationManager lm = (LocationManager) mContext.getSystemService(
          Context.LOCATION_SERVICE); 
        // Toast.makeText(mContext, "Lidt: " + lm.getAllProviders(), Toast.LENGTH_SHORT).show(); 
        lm.addTestProvider(LocationManager.NETWORK_PROVIDER, false, false, false, false, false, 
          true, true, android.location.Criteria.POWER_LOW, android.location.Criteria.ACCURACY_FINE); 
        lm.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, true); 

        LocationManager lm2 = (LocationManager) mContext.getSystemService(
          Context.LOCATION_SERVICE); 
        lm2.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, false, 
          true, true, android.location.Criteria.POWER_LOW, android.location.Criteria.ACCURACY_FINE); 
        lm2.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true); 
        this.hasProvider = true; 
       } 
       isWorking = setMockLocation(location); 
       setMockLocation2(location); 
      }catch (Exception e) 
      { 
       isWorking = false; 
      } 
      return isWorking; 
     } 

    return false; 
} 

public void run() { 
    Thread thread = new Thread() { 
     @Override 
     public void run() { 

       while (true) { 
        try { 
         if (isWorking) { 
          if (currentLocation != null) { 
           setMockLocation(currentLocation); 
           setMockLocation2(currentLocation); 
          } 
         } 
         sleep(1000); 
        } catch (InterruptedException e) { 
         // e.printStackTrace(); 
         Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show(); 
        } 
       } 
     } 
    }; 

    thread.start(); 
} 

/* 
    Stop the spoofed location and return to the source location 
*/ 
public void stopMockLocation() { 
    if (isWorking) { 


     LocationManager lm = (LocationManager) mContext.getSystemService(
       Context.LOCATION_SERVICE); 
     lm.removeTestProvider(LocationManager.NETWORK_PROVIDER); 
     lm.removeTestProvider(LocationManager.GPS_PROVIDER); 
     currentLocation = null; 
     this.isWorking = false; 
    } 
} 

private boolean setMockLocation(com.quinny898.library.persistentsearch.Location location) { 
    try 
    { 
    LocationManager lm = (LocationManager) 
      mContext.getSystemService(Context.LOCATION_SERVICE); 

    android.location.Location newLocation = new android.location.Location(LocationManager.NETWORK_PROVIDER); 

    newLocation.setLatitude(location.getLat()); 
    newLocation.setLongitude(location.getLng()); 
    newLocation.setAccuracy(500); 
    newLocation.setTime(System.currentTimeMillis()); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
     newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); 
    } 
    lm.setTestProviderLocation(LocationManager.NETWORK_PROVIDER, newLocation); 
    } catch (Exception e) { 
     hasProvider = false; 
     return false; 
    } 
    return true; 
} 

private boolean setMockLocation2(com.quinny898.library.persistentsearch.Location location) { 
    try { 
     LocationManager lm = (LocationManager) 
       mContext.getSystemService(Context.LOCATION_SERVICE); 

     android.location.Location newLocation = new android.location.Location(LocationManager.GPS_PROVIDER); 

     newLocation.setLatitude(location.getLat()); 
     newLocation.setLongitude(location.getLng()); 
     newLocation.setAccuracy(500); 
     newLocation.setTime(System.currentTimeMillis()); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
      newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); 
     } // todo 
     lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation); 
    } catch (Exception e) { 
     hasProvider = false; 
     return false; 
    } 

    return true; 
} 

這是一個很好的方式來稱呼它們兩個嗎?

+0

使用Google Play服務新增的位置api。它將消除所有位置管理員的障礙。 – androidnoobdev

回答

0

網絡提供商意味着該位置是基於手機塔和WiFi接入點的可用性。 來自GPS提供商的位置基於衛星,這個基本上有更好的準確性。 幾乎所有的應用程序都更加重視GPS提供商收到的位置。