Last active
December 18, 2015 22:09
-
-
Save kiichi/5852289 to your computer and use it in GitHub Desktop.
Mock Location Class for Android
Credits: http://mobiarch.wordpress.com/2012/07/17/testing-with-mock-location-data-in-android/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center"> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="New York" | |
android:id="@+id/btnNewYork" | |
android:layout_gravity="center_horizontal|top"/> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Florida" | |
android:id="@+id/btnFlorida" | |
android:layout_gravity="center_horizontal|top"/> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="California" | |
android:id="@+id/btnCalifornia" | |
android:layout_gravity="center_horizontal|top"/> | |
</LinearLayout> | |
<fragment | |
android:id="@+id/map" | |
android:name="com.google.android.gms.maps.MapFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.liu.locationexample; | |
import android.content.Context; | |
import android.location.Location; | |
import android.location.LocationManager; | |
import java.lang.reflect.Method; | |
/** | |
* Reference & Credits: http://mobiarch.wordpress.com/2012/07/17/testing-with-mock-location-data-in-android/ | |
* Modified by Kiichi Takeuchi | |
*/ | |
public class MockLocationProvider { | |
String providerName; | |
Context ctx; | |
public MockLocationProvider(String name, Context ctx) { | |
this.providerName = name; | |
this.ctx = ctx; | |
LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); | |
lm.addTestProvider(providerName, false, false, false, false, false, true, true, 0, 5); | |
lm.setTestProviderEnabled(providerName, true); | |
} | |
public void pushLocation(double lat, double lon) { | |
LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); | |
Location mockLocation = new Location(providerName); | |
mockLocation.setLatitude(lat); | |
mockLocation.setLongitude(lon); | |
mockLocation.setAltitude(0); | |
mockLocation.setTime(System.currentTimeMillis()); | |
try { | |
Method method = Location.class.getMethod("makeComplete"); | |
if (method != null) { | |
method.invoke(mockLocation); | |
} | |
} | |
catch (Exception ex){ | |
} | |
lm.setTestProviderLocation(providerName, mockLocation); | |
} | |
public void shutdown() { | |
LocationManager lm = (LocationManager) ctx.getSystemService( | |
Context.LOCATION_SERVICE); | |
lm.removeTestProvider(providerName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.liu.locationexample; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.location.Criteria; | |
import android.location.GpsStatus; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import com.google.android.gms.maps.CameraUpdate; | |
import com.google.android.gms.maps.CameraUpdateFactory; | |
import com.google.android.gms.maps.GoogleMap; | |
import com.google.android.gms.maps.MapFragment; | |
import com.google.android.gms.maps.model.LatLng; | |
import java.lang.reflect.Method; | |
import java.util.Date; | |
public class MyActivity extends Activity { | |
MockLocationProvider mock; | |
/** | |
* Called when the activity is first created. | |
*/ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
LocationManager locationManager = (LocationManager)this.getApplicationContext().getSystemService(Context.LOCATION_SERVICE); | |
mock = new MockLocationProvider(LocationManager.GPS_PROVIDER, this); | |
mock.pushLocation(25.780107,-80.234871); | |
Button btnNewYork = (Button)findViewById(R.id.btnNewYork); | |
btnNewYork.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
mock.pushLocation(40.75662,-73.982148); | |
} | |
}); | |
Button btnFlorida = (Button)findViewById(R.id.btnFlorida); | |
btnFlorida.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
mock.pushLocation(25.780107,-80.234871); | |
} | |
}); | |
Button btnCalifornia = (Button)findViewById(R.id.btnCalifornia); | |
btnCalifornia.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
mock.pushLocation(37.76203,-122.422371); | |
} | |
}); | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1,new LocationListener() { | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.v("myapp", location.toString()); | |
MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map); | |
GoogleMap map = mapFragment.getMap(); | |
map.setMapType(GoogleMap.MAP_TYPE_NORMAL); | |
LatLng centerLatLng = new LatLng(location.getLatitude(),location.getLongitude()); | |
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(centerLatLng, 10); | |
map.moveCamera(cameraUpdate); | |
map.setMyLocationEnabled(true); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
Log.v("myapp", "status = %d"); | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
Log.v("myapp","provider enabled"); | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
Log.v("myapp","provider disabled"); | |
} | |
}); | |
} | |
protected void onDestroy() { | |
mock.shutdown(); | |
super.onDestroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment