-2

我有一个应用程序可以在Chrome自定义选项卡中打开链接。但是,当用户第一次运行它(并且他安装了多个浏览器)时,它会给用户一个弹出窗口,要求他选择默认应用程序(即,您是否想将默认设置为UCBrowser或Chrome等)默认情况下打开应用程序

有什么我可以跳过这个弹出窗口,并始终打开铬我的应用程序?

+1

只是想确保你明白,Chrome可能没有安装在设备上。 –

+1

我们事先有明确的检查。如果chrome没有安装,我们继续在web视图中。 –

回答

1

是的,你可以使用PackageManager这个

String url = "http://www.example.com"; 

PackageManager pm = context.getPackageManager(); 
Intent launchIntent = pm.getLaunchIntentForPackage("com.android.chrome"); 
launchIntent.setData(Uri.parse(url)); 
if (launchIntent != null) { 
    context.startActivity(launchIntent); 
} else { 
    Toast.makeText(context, "Chrome not found", Toast.LENGTH_SHORT).show(); 
} 

或者你也可以只使用setPackage方法上Intent

Intent launchIntent = new Intent(); 
launchIntent.setAction("android.intent.action.VIEW"); 
launchIntent.addCategory("android.intent.category.BROWSABLE"); 
launchIntent.setPackage("com.android.chrome"); 
launchIntent.setData(Uri.parse(url)); 
startActivity(launchIntent); 

,但你必须确保包是否存在setPackage之前(com.android.chrome在你的情况)

相关问题