In Monodroid, I am trying to access GPS services. All I need is a location object.
this line thorws an error:
- _locationManager.RequestLocationUpdates(LocationManager.GpsProvider, 1000, 10, this);
Java.Lang.SecurityException
The stack trace says at Android.Runtime.JNIEnv.CallVoidMethod(IntPtr jobject, IntPtr jmethod, Android.Runtime.jValue[] params) [0x00000] in :0
I have the latest version of monodroid and I am running it on a Samsung Galaxy S II physical device.
I think it has something to do with the Manifest file – I suspect there is an attribute I am missing so that Monodroid can generate a manifest that tells the device I am using GPS services.
I know from http://docs.xamarin.com/android/advanced_topics/working_with_androidmanifest.xml that Monodroid generates it’s own AndroidManifest file using mandroid.exe. Now either I must modify the AndroidManifest.xml file myself or I must get mandroid to insert GPS things for me. Here’s the kicker: there is no ‘merge’ file as described by the link above – so I am left with doing it through mandroid.
So do I create and add a new file for mandroid to merge with?
Here is what I am doing:
...
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Locations;
...
namespace mynamespace
{
[Activity(Label = "My Activity")]
public class MainActivity : Activity, ILocationListener
{
...
#region Location Variables
LocationManager _locationManager;
#endregion
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MainPage);
...
#region Location Setup
try
{
_locationManager = null;
if (/*user says to use location services*/)
{
_locationManager = (LocationManager)GetSystemService(Context.LocationService);
_locationManager.RequestLocationUpdates(LocationManager.GpsProvider, 1000, 10, this);
}
}
catch (Exception ex)
{
_debug.Text = string.Format("{0} -- {1}",ex.Message,ex.StackTrace);
}
#endregion
...
}
#region Common Methods
...
#endregion
#region Events
...
#endregion
#region Location Services
public void OnLocationChanged(Location location)
{
_debug.Text = string.Format("long: {0}, lat: {1}", location.Longitude, location.Latitude);
}
public void OnProviderDisabled(string provider)
{
//throw new NotImplementedException();
}
public void OnProviderEnabled(string provider)
{
//throw new NotImplementedException();
}
public void OnStatusChanged(string provider, int status, Bundle extras)
{
//throw new NotImplementedException();
}
#endregion
}
}
You app does not have permissions to access GPS. To solve this open context menu on project, select Properties-> Android Manifest. If there is no nothing click on the link “No AndroidMaifest.xml found. Click to add one.” Check permission to access GPS data – ACCESS_FINE_LOCATION. Save, rebuild, enjoy.