Android判断包名和类名是否存在的方法
程序员文章站
2023-12-06 10:16:28
1.对包名的判断,异常则说明不存在:复制代码 代码如下: try { packagemanager pm = getpackagemanager(); pm...
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;
}