4

I am working on xamarin.forms. (Facing below problem only in android)

When my application is started it checks my GPS location is on or off.

To check GPS location on or off I am using dependency service.

public static bool CheckGPSConnection()
        {
            var gpsConnection = DependencyService.Get<IGPSConnectivity>();
            return gpsConnection.CheckGPSConnection();
        }

When I come to Home page of my application I put following code

if (Device.OS == TargetPlatform.Android)
{
    if (!App.CheckGPSConnection())
    {
        bool answer = await DisplayAlert("Alert", "Would you like to start GPS?", "Yes", "No");
        if (answer)
        {
              Android.App.Application.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings));
        }
    }
}

but it's giving me an exception

{Android.Util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/…}

What should I do?

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
RMR
  • 599
  • 3
  • 12
  • 35
  • 1
    If you're using Forms you probably want to use `Xamarin.Forms.Forms.Context` – Gerald Versluis Apr 25 '17 at 10:41
  • var intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); – Dilmah Apr 25 '17 at 10:43
  • @Dineshkumar as already pointed out in the error message itself ('Is this really what you want?'), that is not ideal. – Gerald Versluis Apr 25 '17 at 10:47
  • Xamarin.Forms.Forms.Context not found – RMR Apr 25 '17 at 10:56
  • That is weird, it should be there. In what project is this code anyway? The Droid project right? Or are you working with a Shared project instead of a PCL? – Gerald Versluis Apr 25 '17 at 10:59
  • I am working in PCL with xamarin.forms not native. – RMR Apr 25 '17 at 11:01
  • Instead of this line `Android.App.Application.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings));` this line `Xamarin.Forms.Forms.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings));` should be working. – Gerald Versluis Apr 25 '17 at 11:06
  • Yes, I found in dorid project, not in PCL. So to access in PCL I have to make dependency service. right? – RMR Apr 25 '17 at 11:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142601/discussion-between-gerald-versluis-and-rutul-mehta). – Gerald Versluis Apr 25 '17 at 11:09

2 Answers2

11

This is platform specific functionality, so you should create a DependencyService for it.

Just like for the IGPSConnectivity create another interface. For example ISettingsService.

public interface ISettingsService
{
    void OpenSettings();
}

Then on Android, implement it like this:

public class SettingsServiceAndroid : ISettingsService
{
    public void OpenSettings()
    {
        Xamarin.Forms.Forms.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocat‌​ionSourceSettings));
    }
}

Now call it from your shared PCL code, again, just like with the GPS connectivity one.

DependencyService.Get<ISettingsService>().OpenSettings();

Because you are using the DependencyService, the right implementation per platform will be injected. So, there is no need for the if (Device.OS == TargetPlatform.Android) line, unless you did that for another reason of course. Also, I think this method is now deprecated. You should now use Device.RuntimePlatform == Device.Android as of Xamarin.Forms 2.3.4.

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
0

In Android I use this (calling with DependencyServices) to open Settings

    public void View(){
        LocationManager locationManager = (LocationManager)Forms.Context.GetSystemService(Context.LocationService);

        if (locationManager.IsProviderEnabled(LocationManager.GpsProvider) == false)
        {
            Intent gpsSettingIntent = new Intent(Settings.ActionLocationSourceSettings);
            Forms.Context.StartActivity(gpsSettingIntent);
        }
    }
Alessandro Caliaro
  • 5,623
  • 7
  • 27
  • 52