如何在android中使用phonegap打开位置设置

时间:2014-12-08 14:48:02

标签: android cordova phonegap-plugins webintents

我正在尝试使用WebIntent插件为android打开android中的位置设置。我使用GPSDetector插件进行phonegap来检测位置是否处于活动状态,如果它未激活,我想打开位置设置。激活位置后,按“返回”按钮并转到index.html。

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

在这种情况下,我不知道哪个是动作,我试过这样但是没有用。 我做了一个活动,然后在onCreate方法中添加了:

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

但是通过这种方式,我不知道在打开位置(Gps)后如何将用户发送回index.html。 请帮忙,非常感谢

1 个答案:

答案 0 :(得分:0)

我没有使用任何可用的插件。我编写了自己的插件,这是该插件的java代码。检查这是否有用。它适合我的要求。

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());
      }             
  }