2016-02-12 149 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提供商收到的位置。