【问题标题】:Make GPS/Location is compulsory for the app应用程序必须设置 GPS/位置
【发布时间】:2018-02-07 21:01:23
【问题描述】:

我关注this 以编程方式启用 GPS

所以这里它使用确定和取消按钮

所以如果用户按下取消按钮应用程序将退出并且如果他按下确定应用程序将与 GPS 正常工作。

在我的应用程序中,我有 12 个活动,所有活动都需要 GPS(位置)

使用该示例,它可以正常工作,但如果用户手动禁用 GPS 我应该怎么做我需要为所有 12 个活动添加相同的代码...? 任何人都可以建议我如何在每个活动上监控 GPS 状态打开或关闭,如果用户在打开活动后禁用 GPS 应用程序应该关闭...

我已经试过了,但它只能工作一次

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (requestCode == 1000) {
        if(resultCode == Activity.RESULT_OK)
        {
            String result=data.getStringExtra("result"); 
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            finish();
            System.exit(0);
        } 
    } 
} 

【问题讨论】:

    标签: android gps


    【解决方案1】:

    在您的应用程序开始时,让用户使用您编写的代码启用他的 GPS。

    然后,像这样创建一个BroadcastReceiver

    public class GPSChangedReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Toast.makeText(context, "GPS status changed", Toast.LENGTH_SHORT).show();
         // Your code to enable GPS again
         // Give Alert to eneable GPS again
         // Any other task that you want to perform
        }
    }
    

    并将其注册到您的manifest.xml

    <receiver android:name=".GPSChangedReceiver">
                <intent-filter>
                    <action android:name="android.location.PROVIDERS_CHANGED" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </receiver>
    

    这个BroadcastReceiver会在用户每次改变他的GPS状态时被调用,所以现在每当用户试图打开/关闭他的GPS时,广播就会被调用,你可以给他警报对话或强迫他启用他的全球定位系统。

    【讨论】:

      【解决方案2】:

      仅在应用启动时(例如onResume),在AndroidManifest.xml 中定义了intent-filter 的活动中请求权限。

      【讨论】:

        【解决方案3】:

        创建一个 BaseActivity 并通过此 BaseActivity 扩展所有 12 个活动

        在基地做所有与位置相关的工作

        【讨论】:

        • onActivityResult 处理需要用于其他目的时,该方法可能会不方便。
        【解决方案4】:

        您可以创建 BaseActivity.class 并在您的所有活动中扩展它。然后在 BaseActivity 的 onResume 中添加位置检查对话框并相应地导航用户。

        【讨论】:

          【解决方案5】:

          使用此方法是否启用检查位置。 你可以直接在onStart()调用这个方法

          int locationMode = 0;
                  String locationProviders;
          
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                      try {
                          locationMode = Settings.Secure.getInt(
                                  context.getContentResolver(),
                                  Settings.Secure.LOCATION_MODE);
          
                      } catch (Settings.SettingNotFoundException e) {
                          e.printStackTrace();
                      }
          
                      return locationMode != Settings.Secure.LOCATION_MODE_OFF;
          
                  } else {
                      locationProviders = Settings.Secure.getString(
                              context.getContentResolver(),
                              Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
                      return !TextUtils.isEmpty(locationProviders);
                  }
          

          返回该值,如果未启用,则使用以下方法

           public void showGPSAlert() {
                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle(R.string.location_not_enabled); // GPS not found
                  builder.setMessage(R.string.location_access_enable); // Want to enable?
                  builder.setPositiveButton(R.string.ok,
                          new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialogInterface, int i) {
                                  Intent viewIntent = new Intent(
                                          Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                  startActivityForResult(viewIntent, LOCATION_ENABLED);
                              }
                          });
                  builder.create().show();
                  return;
          
              }
          

          【讨论】:

            【解决方案6】:

            在一个共同的地方(比如你所有 12 个活动的 BaseActivity) 您可以注册收听GPS状态

            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            lm.addGpsStatusListener(new android.location.GpsStatus.Listener()
            {
                public void onGpsStatusChanged(int event)
                {
                    switch(event)
                    {
                    case GPS_EVENT_STARTED:
                        // GPS is switched ON
                        break;
                    case GPS_EVENT_STOPPED:
                        // GPS is switched off
                        break;
                    }
                }
            });
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多