xamarin android how to turn Off GPS ?

AtlantisDe 41 Reputation points
2021-09-11T18:18:00.43+00:00

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);
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,491 Reputation points Microsoft Vendor
    2021-09-13T06:00:45.93+00:00

    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.


1 additional answer

Sort by: Most helpful
  1. AtlantisDe 41 Reputation points
    2021-09-22T10:53:47.823+00:00
    0 comments No comments