Sylvain
Sylvain

Reputation: 293

I'd like to open the app settings page from a Xamarin Android app

I need to open the app settings page for my Xamarin Android app.

Using Java, it seems that the correct way to do it is:

startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
              Uri.parse("package:" + BuildConfig.APPLICATION_ID)));

So, using C#, I tried:

StartActivity(new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings,
              Android.Net.Uri.Parse("package:" + BuildConfig.ApplicationId)));

This does nothing... I tried without the Uri parameter, and in that case I get an exception:

Android.Content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.APPLICATION_DETAILS_SETTINGS }

I also tried

StartActivityForResult(
    new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings), 0);

Same exception...

Any idea?

Thanks.

Upvotes: 4

Views: 7525

Answers (4)

Wouter Klopper
Wouter Klopper

Reputation: 111

Xamarin.Essentials.AppInfo.ShowSettingsUI(); 

seems to work.

Upvotes: 11

Junaid Pathan
Junaid Pathan

Reputation: 4306

Using @Sylvain and @Mikish answer,

I got an Exception:

Android.Util.AndroidRuntimeException: 'Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

Hence slightly modifying the answer:

public void OpenSettings()
{
    Intent intent = new Intent(
                    Android.Provider.Settings.ActionApplicationDetailsSettings,
                    Android.Net.Uri.Parse("package:" + Application.Context.PackageName));
    intent.AddFlags(ActivityFlags.NewTask);

    Application.Context.StartActivity(intent);
}

Upvotes: 1

Mikish
Mikish

Reputation: 31

Using Xamarin through Visual Studio and C#, I switched to Application.Context.PackageName too.

I kept finding Java examples using BuildConfig.ApplicationId, but in C#, that caused me to run into your same problems. Our goals are different, but I wanted to affirm that your syntax got me one step further towards mine.

using Android.App;
using Android.Net;
...
StartActivity(new Intent(
    Android.Provider.Settings.ActionApplicationDetailsSettings, 
    Uri.Parse("package:" + Application.Context.PackageName)));

Upvotes: 3

Sylvain
Sylvain

Reputation: 293

I finally found the issue!

In

StartActivity(new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings,
          Android.Net.Uri.Parse("package:" + BuildConfig.ApplicationId)));

It's the

BuildConfig.ApplicationId

that doesn't work...

The correct call (or at least the one that worked for me) using Xamarin is then

StartActivity(new Intent(
    Android.Provider.Settings.ActionApplicationDetailsSettings,
    Android.Net.Uri.Parse("package:"+ Android.App.Application.Context.PackageName)));

Upvotes: 16

Related Questions