【问题标题】:Opening Android Settings programmatically以编程方式打开 Android 设置
【发布时间】:2013-10-31 07:07:15
【问题描述】:

如何以编程方式打开设置?

【问题讨论】:

    标签: android kotlin android-settings


    【解决方案1】:

    遵循https://developer.android.com/training/permissions/requesting 上描述的新 api

    private val goToSettingsRequest = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { activityResult ->
        // TODO: DEAL WITH THE CALLBACK
    }
    
    private fun goToSettings() {
        goToSettingsRequest.launch(Intent(Settings.ACTION_SETTINGS))
    }
    

    【讨论】:

      【解决方案2】:

      我使用了most upvoted answer中的代码:

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

      在同一窗口中打开设备设置,因此我的 android 应用程序 (finnmglas/Launcher) 的 android 用户卡在了那里。

      2020 年及以后的答案(在 Kotlin 中):

      startActivity(Intent(Settings.ACTION_SETTINGS))
      

      它适用于我的应用程序,也应该适用于您的应用程序,而不会产生任何不良后果。

      【讨论】:

      • 谢谢朋友,请像这样更新您的代码:startActivity(new Intent(Settings.ACTION_SETTINGS));
      • 7年后解决方案出来了。
      • 这里的“设置”导入是 import android.provider.Settings
      【解决方案3】:

      使用警报对话框以编程方式打开 android 位置设置

      AlertDialog.Builder alertDialog = new AlertDialog.Builder(YourActivity.this);
      alertDialog.setTitle("Enable Location");
      alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
      alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                  startActivity(intent);
            }
      });
      alertDialog.show();
      

      【讨论】:

        【解决方案4】:

        如果有人发现此问题并且您想为您的特定应用程序打开设置:

            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            intent.data = Uri.parse("package:" + context.packageName)
            startActivity(intent)
        

        【讨论】:

          【解决方案5】:

          这是为我做的

          Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
          startActivityForResult(callGPSSettingIntent);
          

          当他们按下返回时,它会返回到我的应用程序。

          【讨论】:

          • 这提供了哪些设置屏幕?
          • @IgorGanapolsky 它在“设置”中打开基于位置的设置
          【解决方案6】:

          使用定位的包将用户发送到设置,例如 WRITE_SETTINGS 权限:

          startActivityForResult(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS).setData(Uri.parse("package:"+getPackageName()) ),0);
          

          【讨论】:

            【解决方案7】:

            查看Programmatically Displaying the Settings Page

                startActivity(context, new Intent(Settings.ACTION_SETTINGS), /*options:*/ null);
            

            通常,您使用预定义的常量Settings.ACTION__SETTINGS。完整列表可以在here找到。

            【讨论】:

            • 有没有办法在新选项中打开设置:“控制哪些应用程序可以读取您的通知”(在 API 18 中添加)?
            【解决方案8】:

            使用此意图在 android 设备的设置应用中打开安全和位置屏幕

                startActivity(new Intent(Settings.ACTION_SECURITY_SETTINGS));
            

            【讨论】:

              【解决方案9】:

              您可以尝试调用:

              startActivityForResult(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
              

              设置画面中的其他画面,可前往

              https://developer.android.com/reference/android/provider/Settings.html

              希望在这种情况下对您有所帮助。

              【讨论】:

              • 您需要将“REQUEST_CODE”作为startActivityForResult 的第二个参数传递。
              【解决方案10】:

              你可以另开一个班级来做这种活动。

              public class Go {
              
                 public void Setting(Context context)
                  {
                      Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
                      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      context.startActivity(intent);
                  }
              }
              

              【讨论】:

                【解决方案11】:

                要实现这一点,只需使用 Intent 并使用常量 ACTION_SETTINGS,专门定义为显示系统设置:

                startActivity(new Intent(Settings.ACTION_SETTINGS));
                

                startActivityForResult() 是可选的,仅当您想在设置活动关闭时返回一些数据时。

                startActivityForResult(new Intent(Settings.ACTION_SETTINGS), 0);
                

                here您可以找到内容列表以显示应用程序的特定设置或详细信息。

                【讨论】:

                  【解决方案12】:

                  你可以打开

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

                  您可以通过按设备上的返回按钮返回。

                  【讨论】:

                  • 当用户按下清除缓存按钮时有什么方法可以返回?
                  • @SweetWisherヅ你只需要编辑源代码。
                  • 现在你可以使用 Intent intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);开始活动(意图);每个主要设置类别都有大量常量可供您选择。只写设置。 Android Studio 会在自动完成中显示所有类别。
                  • 我可以在设置应用程序中搜索特定设置并从我的应用程序以编程方式打开该特定设置吗?例如我可以在手机设置中的应用中搜索 OTG 吗?
                  • 在我看来使用 StartActivity 就足够了,当我回到应用程序时总是得到结果 CANCELD(当返回按钮返回时这是正常的)。因此,应用程序不会以这种方式收到更改设置的通知。
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-11-27
                  • 1970-01-01
                  • 2013-02-19
                  • 2017-08-21
                  • 2016-08-29
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多