I'm trying to d开发者_运维技巧etermine if a generic class object is an instance of an abstract class. So far I'm not having much luck. Below is the code I'm trying to use. AbstractActivity is the name of a parent class I extend some of my activities from.
public void startActivity(Intent intent)
{
ComponentName name = intent.getComponent();
if(name != null)
{
Class<?> cls = null;
try {
cls = Class.forName(name.getClassName());
if(cls.isInstance(AbstractActivity));
{
//do something
}
else
{
super.startActivity(intent);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.startActivity(intent);
}
I would try:
if(AbstractActivity.class.isAssignableFrom(cls)) {
....
}
You can base on simplename of class.
this.getClass().getSimpleName().equals(LauncherActivity.class.getSimpleName())
精彩评论