PackageManager resolveActivity查询是否有符合条件的Activity

Activity间通过隐式intent跳转,在发出intent之前必须通过resolveActivity检查,避免找不到合适的调用组件,造成ActivityNotFoundException的异常


  1. Intent intent = new Intent(Intent.ACTION_VIEW);  
  2.             intent.setDataAndType(Uri.parse(url), mimetype);  
  3.             if (getPackageManager().resolveActivity(intent,  
  4.                         PackageManager.MATCH_DEFAULT_ONLY) != null) {  
  5.                 // someone knows how to handle this mime type with this scheme, don't download.  
  6.                 try {  
  7.                     startActivity(intent);  
  8.                     return;  
  9.                 } catch (ActivityNotFoundException ex) {  
  10.                     if (Config.LOGD) {  
  11.                         Log.d(LOGTAG, "activity not found for " + mimetype  
  12.                                 + " over " + Uri.parse(url).getScheme(), ex);  
  13.                     }  
  14.                       
  15.                 } 

猜你喜欢

转载自blog.csdn.net/xiaodongvtion/article/details/79471645