【问题标题】:how can i open location settings in android using phonegap如何使用phonegap在android中打开位置设置
【发布时间】:2014-12-08 14:48:02
【问题描述】:

我正在尝试使用用于 phonegap 的 WebIntent 插件在 android 中打开位置设置。我为 phonegap 使用 GPSDetector 插件来检测位置是否处于活动状态,如果它不处于活动状态,我想打开位置设置。激活位置后按返回按钮并转到 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);

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

【问题讨论】:

    标签: android cordova phonegap-plugins webintents


    【解决方案1】:

    我没有使用任何可用的插件。我编写了自己的插件,这是该插件的 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());
          }             
      }
    

    【讨论】:

    • 谢谢你的回答,我昨天几乎和你一样解决了这个问题,从我的 CordovaActivity 和 android 原生代码。
    猜你喜欢
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    相关资源
    最近更新 更多