question

92696164 avatar image
0 Votes"
92696164 asked LeonLu-MSFT edited

xamarin android how to turn Off GPS ?

xamarin android how to turn Off GPS ?

i find the android code ...
but i can not translation to C# ths...

Code

 public void turnGPSOn()
 {
      Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
      intent.putExtra("enabled", true);
      this.ctx.sendBroadcast(intent);
    
     String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
     if(!provider.contains("gps")){ //if gps is disabled
         final Intent poke = new Intent();
         poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
         poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
         poke.setData(Uri.parse("3")); 
         this.ctx.sendBroadcast(poke);
    
    
     }
 }
 // automatic turn off the gps
 public void turnGPSOff()
 {
     String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
     if(provider.contains("gps")){ //if gps is enabled
         final Intent poke = new Intent();
         poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
         poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
         poke.setData(Uri.parse("3")); 
         this.ctx.sendBroadcast(poke);
     }
 }



dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

Hello 德哥,​

Welcome to our Microsoft Q&A platform!

Your achievement comes from a exploite a bug in the power manager widget, But this Bug is fixed, Please see the SilithCrowe's Comment.

https://stackoverflow.com/a/5305835

If you still want to translate it to C# code. Here is your needs.

class GPSService : IGPSService
    {
        public void turnGPSOff()
        {
            Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
            intent.PutExtra("enabled", true);

            MainActivity.Instance.SendBroadcast(intent);
            bool gps_enabled = false;
         
            if (Build.VERSION.SdkInt >= BuildVersionCodes.P)
            {
                // This is new method provided in API 28
                LocationManager lm = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
               

                try
                {
                    gps_enabled = lm.IsProviderEnabled(LocationManager.GpsProvider);

                    if (!gps_enabled)
                    {
                        //if gps is disabled
                        Intent poke = new Intent();
                        poke.SetClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
                        poke.AddCategory(Intent.CategoryAlternative);
                        poke.SetData(Android.Net.Uri.Parse("3"));

                        MainActivity.Instance.SendBroadcast(poke);
                    }

                }
                catch (Exception ex) { }
            }
            else
            {
                // This is Deprecated in API 28
                string provider = Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.LocationProvidersAllowed);
                if (!provider.Contains("gps"))
                { //if gps is disabled
                     Intent poke = new Intent();
                    poke.SetClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
                    poke.AddCategory(Intent.CategoryAlternative);
                    poke.SetData(Android.Net.Uri.Parse("3"));
                
                    MainActivity.Instance.SendBroadcast(poke);

                }
            }


           
        }

        public void turnGPSOn()
        {
           

            string provider = Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.LocationProvidersAllowed);
            if (!provider.Contains("gps"))
            { //if gps is disabled
                Intent poke = new Intent();
                poke.SetClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
                poke.AddCategory(Intent.CategoryAlternative);
                poke.SetData(Android.Net.Uri.Parse("3"));

                MainActivity.Instance.SendBroadcast(poke);

            }
        }
    }


PS: MainActivity.Instance is a static property set value in the OnCreate method.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public static MainActivity Instance;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Instance = this;
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        
        }


But I want to recommand you to use following ways to enable the GPS location. It will open the location settings in System settings.

private void ButtonEnableGPS_Click(object sender, System.EventArgs e)
        {
            //throw new System.NotImplementedException();

            LocationManager lm = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
            bool gps_enabled = false;

            try
            {
                gps_enabled = lm.IsProviderEnabled(LocationManager.GpsProvider);

                if (!gps_enabled)
                {
                    //if gps is disabled


                    Intent intent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings);
                    this.StartActivity(intent);
                }

            }
            catch (Exception ex) { }
        }


131379-image.png

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (49.5 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@92696164 May I know if you have got any chance to check my answer? I am glad to help if you have any other questions

0 Votes 0 ·