如果是在Service或者线程中调用,需要在Manifest.xml文件中添加权限:
<uses-permission android:name=“android.permission.GET_TASKS” />
//判断当前界面显示的是哪个Activity public static String getTopActivity(Context context){ ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity; Log.d("测试", "pkg:"+cn.getPackageName());//包名 Log.d("测试", "cls:"+cn.getClassName());//包名加类名 return cn.getClassName(); }