如何在我的浏览器应用程序中打开默认浏览器应用程序对话框



我是android开发的新手,有一个问题。

我想向用户显示"更改默认浏览器应用程序"对话框。在该对话框中,将显示所有浏览器应用程序,用户可以选择一个他/她想要使用的默认浏览器应用程序。

所以我的问题是;如何在我的浏览器应用程序中打开默认浏览器应用程序对话框">

请参阅下面所附的对话框截图供您参考。

默认对话框屏幕截图

适用于Android>=Q使用RoleManagerROLE_BROWSER角色。

对于Android<Q和>=N将用户导航到Android默认应用程序设置。

对于Android<打开帮助页面,向用户展示如何更改默认应用程序。

public class MainActivity extends AppCompatActivity {
private static final String SETTINGS_SELECT_OPTION_KEY = ":settings:fragment_args_key";
private static final String SETTINGS_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
private static final String DEFAULT_BROWSER_APP_OPTION = "default_browser";
private final ActivityResultLauncher<Intent> resultLauncher =
registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Toast.makeText(MainActivity.this, "Role is granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Role is not granted", Toast.LENGTH_LONG).show();
}
}
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);
Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_BROWSER);
resultLauncher.launch(intent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
intent.putExtra(
SETTINGS_SELECT_OPTION_KEY,
DEFAULT_BROWSER_APP_OPTION
);
Bundle bundle = new Bundle();
bundle.putString(SETTINGS_SELECT_OPTION_KEY, DEFAULT_BROWSER_APP_OPTION);
intent.putExtra(
SETTINGS_SHOW_FRAGMENT_ARGS,
bundle
);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
// Show some help page
}
}
}

相关内容

最新更新