Android判断包名和类名是否存在的方法
内容摘要
1.对包名的判断,异常则说明不存在:复制代码 代码如下: try { PackageManager pm = getPackageManager(); pm.getPackageInfo("com.org", PackageManager
文章正文
1.对包名的判断,异常则说明不存在:
try {
PackageManager pm = getPackageManager();
pm.getPackageInfo("com.org", PackageManager.GET_ACTIVITIES);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2.对类名的判断,异常则说明不存在:
try {
Class.forName("com.org.MainActivity");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
return;
}
复制代码 代码如下:
try {
PackageManager pm = getPackageManager();
pm.getPackageInfo("com.org", PackageManager.GET_ACTIVITIES);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2.对类名的判断,异常则说明不存在:
复制代码 代码如下:
try {
Class.forName("com.org.MainActivity");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
return;
}
代码注释