/**
* 调用三方浏览器下载并打开APK
*@paramcontext
*@paramurl 要浏览的资源地址
*/
publicstaticvoidopenBrowser(Context context,String url){
finalIntent intent =newIntent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
// 注意此处的判断intent.resolveActivity()可以返回显示该Intent的Activity对应的组件名
// 官方解释 : Name of the component implementing an activity that can display the intent
if(intent.resolveActivity(context.getPackageManager()) !=null) {
finalComponentName componentName = intent.resolveActivity(context.getPackageManager());// 打印Log ComponentName到底是什么 L.d("componentName = " + componentName.getClassName());
context.startActivity(Intent.createChooser(intent,"请选择浏览器"));
}else{
Toast.makeText(context.getApplicationContext(),"请下载浏览器", Toast.LENGTH_SHORT).show();
}
}
检测手机是否有浏览器:
方法一:
public boolean isExitBrowser(Context context,String url){
Uri uri = Uri.parse(url);
Intent intent =new Intent(Intent.ACTION_VIEW,uri);
intent.setComponent(null);
List rList = context.getPackageManager().
queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY|
PackageManager.GET_RESOLVED_FILTER);
final int browsersize = rList.size();
if(browsersize>0){
// for(int i=0;i
// Log.i(TAG,"i= " + rList.get(i).activityInfo.name);
return true;
}
return false;
}
方法二:
@SuppressLint("WrongConstant")
private boolean hasBrowser(Context context){
PackageManager pm = context.getPackageManager();
Intent intent =new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://"));
List list = pm.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS);
final int size = (list ==null) ?0 : list.size();
return size >0;
}