[英]How to open Default Browser App dialog in my browser app
我是 android 開發的新手,有一個問題。
我想向用戶顯示一個更改默認瀏覽器應用程序對話框。 在該對話框中將顯示所有瀏覽器應用程序,用戶可以 select 一個他/她想要使用的默認瀏覽器應用程序。
所以我的問題是,“如何在我的瀏覽器應用程序中打開默認瀏覽器應用程序對話框?”
請參閱下面附上的對話框截圖供您參考。
對於 Android >= Q 使用具有RoleManager
角色的ROLE_BROWSER
。
對於 Android < Q 和 >= N,將用戶導航到 Android 默認應用程序設置。
對於 Android < N 打開幫助頁面向用戶展示如何更改默認應用程序。
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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.