how can i open location settings in android using phonegap

1.3k Views Asked by At

I am trying to open location settings in android using WebIntent plugin for phonegap. I use GPSDetector plugin for phonegap to detect if location is active and if it is not active i want to open Location Settings. After activate the location press Back button and go to index.html.

window.plugins.webintent.startActivity({
      action: 'android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS'},
      function() {},
      function() {alert('Failed to open URL via Android Intent')}
 );

In this case I don`t know which is the action, I have tried like this but did not work. I have made an activity and there i added in onCreate method :

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

But in this way i don`t know how to send the user back to index.html after he turns on the Location(Gps). Please help and Thanks a lot

1

There are 1 best solutions below

1
On

I didn't use any available plugins. I wrote my own plugin and this is the java code of that plugin. Check if this is helpful. It works as your requirement for me.

if (CHECK_LOCATION_SERVICES_ENABLED.equals(action))
  {
      try 
      {   boolean locationServicesEnabled=false;
          if(this.ctx.getContext()!=null)
          {
              //Location services not enabled

              final Context context = this.ctx.getContext();
              (new Thread(new Runnable() {

                  @Override
                  public void run() {
                      Looper.prepare();

                      final Handler innerHandler = new Handler() {
                          @Override
                          public void handleMessage(Message message) {
                              /*Toast.makeText(myContext,android.os.Build.VERSION.RELEASE,
                              Toast.LENGTH_LONG).show();*/                                                                
                              LocationManager lm = null;
                              boolean gps_enabled=false,network_enabled=false;
                                 if(lm==null)
                                     lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                                 try{
                                 gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
                                 }catch(Exception ex){}
                                 try{
                                 network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                                 }catch(Exception ex){}

                                if(!gps_enabled && !network_enabled){
                                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                                            context);

                                    alertDialog.setTitle("FindMe");

                                    alertDialog
                                            .setMessage("You must enable Location Services for using FindMe.\nDo you want to go to Location Sources Settings?");

                                    alertDialog.setPositiveButton("Settings",
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int which) {
                                                    Intent intent = new Intent(
                                                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                                    context.startActivity(intent);
                                                }
                                            });

                                    alertDialog.setNegativeButton("Cancel",
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.cancel();
                                                }
                                            });

                                    alertDialog.show();

                                 }
                          }

                          @Override
                          public void dispatchMessage(Message message) {
                              handleMessage(message);
                          }
                      };

                      Message message = innerHandler.obtainMessage();
                      innerHandler.dispatchMessage(message);
                      Looper.loop();
                  }
              })).start();

          }
          return new PluginResult(PluginResult.Status.OK,"success ");               
      }                    
      catch (Exception ex)
      {                          
           Log.d("FindMePlugin", ex.toString());
           return new PluginResult(PluginResult.Status.ERROR, "error"+ex.toString());
      }             
  }